如何手动将带有字符的刻度线添加到直方图?
How to add a tick mark with characters manually to a histogram?
我想在 ggplot 中的直方图的 x 轴末尾添加一个刻度线。坐标轴范围是0 - 55,我想把最后一个刻度线改成“>55”。
set1 %>%
ggplot(aes(x=total, fill=lowcost))+
geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+
scale_color_grey()+scale_fill_grey(start = .85,
end = .85,) +
theme_linedraw()+
guides(fill = "none", cols='none')+
geom_vline(aes(xintercept=10, size='Low target'),
color="black", linetype=5)+
geom_vline(aes(xintercept=50, size='High target'),
color="black", linetype="dotted")+
scale_size_manual(values = c(.5, 0.5), guide=guide_legend(title = "Target", override.aes = list(linetype=c(3,5), color=c('black', 'black'))))+
scale_y_continuous(labels=scales::percent)+
scale_x_continuous(breaks = seq(0,55,10),limits = c(0,60))+
facet_grid(cols = vars(lowcost))+
ggtitle("Ask Set 1 ")+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Total donation ($)")+
ylab("Percent")
为了向您展示@Allan Cameron 在评论中所说的内容,我创建了一个可重现的数据集。您可以使用以下代码:
library(tidyverse)
set1 <- data.frame(total = runif(100, 0, 100),
lowcost = rep(LETTERS[1:2], 100))
set1 %>%
ggplot(aes(x=total, fill=lowcost))+
geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+
scale_color_grey()+scale_fill_grey(start = .85,
end = .85,) +
theme_linedraw()+
guides(fill = "none", cols='none')+
geom_vline(aes(xintercept=10, size='Low target'),
color="black", linetype=5)+
geom_vline(aes(xintercept=50, size='High target'),
color="black", linetype="dotted")+
scale_size_manual(values = c(.5, 0.5), guide=guide_legend(title = "Target", override.aes = list(linetype=c(3,5), color=c('black', 'black'))))+
scale_y_continuous(labels=scales::percent)+
scale_x_continuous(breaks = c(seq(0,50,10), 55), labels = c(seq(0, 50, 10), '>55'), limits = c(0, 60)) +
facet_grid(cols = vars(lowcost))+
ggtitle("Ask Set 1 ")+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Total donation ($)")+
ylab("Percent")
输出:
我想在 ggplot 中的直方图的 x 轴末尾添加一个刻度线。坐标轴范围是0 - 55,我想把最后一个刻度线改成“>55”。
set1 %>%
ggplot(aes(x=total, fill=lowcost))+
geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+
scale_color_grey()+scale_fill_grey(start = .85,
end = .85,) +
theme_linedraw()+
guides(fill = "none", cols='none')+
geom_vline(aes(xintercept=10, size='Low target'),
color="black", linetype=5)+
geom_vline(aes(xintercept=50, size='High target'),
color="black", linetype="dotted")+
scale_size_manual(values = c(.5, 0.5), guide=guide_legend(title = "Target", override.aes = list(linetype=c(3,5), color=c('black', 'black'))))+
scale_y_continuous(labels=scales::percent)+
scale_x_continuous(breaks = seq(0,55,10),limits = c(0,60))+
facet_grid(cols = vars(lowcost))+
ggtitle("Ask Set 1 ")+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Total donation ($)")+
ylab("Percent")
为了向您展示@Allan Cameron 在评论中所说的内容,我创建了一个可重现的数据集。您可以使用以下代码:
library(tidyverse)
set1 <- data.frame(total = runif(100, 0, 100),
lowcost = rep(LETTERS[1:2], 100))
set1 %>%
ggplot(aes(x=total, fill=lowcost))+
geom_histogram(binwidth=1,aes(y = (..count..)/sum(..count..)),col=I("black"))+
scale_color_grey()+scale_fill_grey(start = .85,
end = .85,) +
theme_linedraw()+
guides(fill = "none", cols='none')+
geom_vline(aes(xintercept=10, size='Low target'),
color="black", linetype=5)+
geom_vline(aes(xintercept=50, size='High target'),
color="black", linetype="dotted")+
scale_size_manual(values = c(.5, 0.5), guide=guide_legend(title = "Target", override.aes = list(linetype=c(3,5), color=c('black', 'black'))))+
scale_y_continuous(labels=scales::percent)+
scale_x_continuous(breaks = c(seq(0,50,10), 55), labels = c(seq(0, 50, 10), '>55'), limits = c(0, 60)) +
facet_grid(cols = vars(lowcost))+
ggtitle("Ask Set 1 ")+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Total donation ($)")+
ylab("Percent")
输出: