r - 在 ggplot2 中为分类数据创建次要网格线

标签 r ggplot2 themes gridlines

我想将次要网格线添加到使用 ggplot2 生成的绘图的分类数据中。例如,

library("ggplot2")
data<-data.frame(xcategory=rep(c("a","b","c"),each=30), yvalue=c(1,3,6))

ggplot(data,aes(x=xcategory,y=yvalue,alpha=1.0,size=5))+
                geom_point(position=position_jitter(width=0.1,height=0.0))+
                theme_bw()+
                scale_x_discrete(name="Categorical Data") +
                scale_y_continuous(name="Continuous Response",limits=c(0,7)) +
                theme(axis.text.x=element_text(angle = 90),legend.position="none")

生成以下图表:

What I don't want

但我想要的是分类网格线绑定(bind)类别名称,以便我可以抖动网格线内的所有数据。这是我用红色进行编辑的油漆示例:

What I want

以“b”为边界的红线可能就是我所需要的,我只是想说明三个等宽的列。

非常感谢我错过的类似问题的帮助或指导。我尝试通过“主题”添加网格信息但没有成功。

最佳答案

您可以使用 geom_vline()添加线条来绘制并使用 0.5、1.5 等数字来设置位置。数字是从 0.5 开始并递增 1 直到“类别数”+0.5 的向量。这些行将在类别名称之间。

ggplot(data,aes(x=xcategory,y=yvalue,alpha=1.0,size=5))+
  geom_vline(xintercept=c(0.5,1.5,2.5,3.5),color="red")+
  geom_point(position=position_jitter(width=0.1,height=0.0))+
  theme_bw()+
  scale_x_discrete(name="Categorical Data") +
  scale_y_continuous(name="Continuous Response",limits=c(0,7)) +
  theme(axis.text.x=element_text(angle = 90),legend.position="none")

enter image description here

关于r - 在 ggplot2 中为分类数据创建次要网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16570026/

相关文章:

r - summary.rms(sfit) : adjustment values not defined here or with datadist for some variables 中的错误

r - 如何用 R 重现这个移动分布图?

c++ - 从 R 中的一组观察结果创建频率 vector 的矢量化方法?

cakephp - 如何为错误页面使用自定义主题?

ASP.NET 主题示例/入门工具包

r - 错误 : Must supply a symbol or a string as argument

R ggplot2 在使用 facet_grid 时使用 ..count..

r - 如何增加ggplot2中的条形大小?

r - 在相关矩阵中包装轴标签

html - 删除 Tumblr 主题上的 PostID