ggplot直方图中的不等空间
unequal spaces in ggplot histogram
我想用 ggplot 生成直方图,我有数据框 "plot_data"
plot_data<-data.frame(CLV.decile=unique(subset.customer_data$CLV.decile),
CLV=unique(subset.customer_data$mean.CLV.decile))
看起来像这样
> plot_data
CLV.decile CLV
1 10 1560
2 5 1525
3 1 1512
剧情是这样的:
ylim <- c(0, 1.1*max(plot_data$CLV))
ggplot(plot_data, aes(x=CLV.decile, y=CLV)) +
geom_histogram(stat="identity",fill="skyblue",colour="black") +
labs(x="Decile",y="CLV") + geom_text(aes(label=CLV), vjust=-1) + ylim(ylim) +
scale_x_reverse(breaks = plot_data$CLV.decile)
我该如何解决?提前致谢
问题是你的 x 轴是连续的。尝试使用:
library(ggplot2)
ylim <- c(0, 1.1*max(plot_data$CLV))
ggplot(plot_data, aes(x=as.factor(CLV.decile), y=CLV)) +
geom_histogram(stat="identity",fill="skyblue",colour="black") +
labs(x="Decile",y="CLV") + geom_text(aes(label=CLV), vjust=-1) + ylim(ylim) +
scale_x_discrete(limits=as.character(plot_data$CLV.decile))
您需要将 CLV.decile
作为因素,然后提供 scale_x_discrete
来指定顺序。
并且条形之间的间距相等。
编辑:
这本质上是一个条形图,因此您不妨考虑将 geom_histogram
更改为 geom_bar
,但输出不变。
我想用 ggplot 生成直方图,我有数据框 "plot_data"
plot_data<-data.frame(CLV.decile=unique(subset.customer_data$CLV.decile),
CLV=unique(subset.customer_data$mean.CLV.decile))
看起来像这样
> plot_data
CLV.decile CLV
1 10 1560
2 5 1525
3 1 1512
剧情是这样的:
ylim <- c(0, 1.1*max(plot_data$CLV))
ggplot(plot_data, aes(x=CLV.decile, y=CLV)) +
geom_histogram(stat="identity",fill="skyblue",colour="black") +
labs(x="Decile",y="CLV") + geom_text(aes(label=CLV), vjust=-1) + ylim(ylim) +
scale_x_reverse(breaks = plot_data$CLV.decile)
我该如何解决?提前致谢
问题是你的 x 轴是连续的。尝试使用:
library(ggplot2)
ylim <- c(0, 1.1*max(plot_data$CLV))
ggplot(plot_data, aes(x=as.factor(CLV.decile), y=CLV)) +
geom_histogram(stat="identity",fill="skyblue",colour="black") +
labs(x="Decile",y="CLV") + geom_text(aes(label=CLV), vjust=-1) + ylim(ylim) +
scale_x_discrete(limits=as.character(plot_data$CLV.decile))
您需要将 CLV.decile
作为因素,然后提供 scale_x_discrete
来指定顺序。
并且条形之间的间距相等。
编辑:
这本质上是一个条形图,因此您不妨考虑将 geom_histogram
更改为 geom_bar
,但输出不变。