使用GGPLOT2离散量表在X轴上进行间距
我已经使用此代码了一段时间,但这是我第一次在X轴上只有两个类别。由于某种原因,R不会将垃圾箱放在垃圾箱中,而是在一开始就将它们捆成。如何沿X轴均匀地散布它们?
xlabels<-c("A","B")
CTplot <- ggplot(CTsum, aes(x=Treatment, y=FI)) +
geom_col(fill="lightsteelblue") +
scale_x_discrete(labels= xlabels)+xlab("")+ylab("")+
theme(panel.background = element_blank(),axis.line = element_line(colour = "black"),axis.text.x = element_text(size=20,color="black"), axis.text.y = element_text(size=20, color="black"), axis.title.x = element_text(size=25), axis.title.y = element_text(size=25))+
geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width=0.2))+
annotate("text",x=1,y=0.84, label="A",size=5)+
annotate("text",x=2,y=0.62, label="B",size=5)+
annotate("text",x=3,y=0.29, label="",size=5)+
annotate("text",x=4,y=0.26, label="",size=5)+
annotate("text",x=0.65,y=0.5, label="",size=15)
CTplot
I've been working with this code for a while but this is the first time I've only had two categories on my x-axis. For some reason, R will not space out the bins and instead bunches them at the beginning. How do I evenly spread them along the x-axis?
xlabels<-c("A","B")
CTplot <- ggplot(CTsum, aes(x=Treatment, y=FI)) +
geom_col(fill="lightsteelblue") +
scale_x_discrete(labels= xlabels)+xlab("")+ylab("")+
theme(panel.background = element_blank(),axis.line = element_line(colour = "black"),axis.text.x = element_text(size=20,color="black"), axis.text.y = element_text(size=20, color="black"), axis.title.x = element_text(size=25), axis.title.y = element_text(size=25))+
geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width=0.2))+
annotate("text",x=1,y=0.84, label="A",size=5)+
annotate("text",x=2,y=0.62, label="B",size=5)+
annotate("text",x=3,y=0.29, label="",size=5)+
annotate("text",x=4,y=0.26, label="",size=5)+
annotate("text",x=0.65,y=0.5, label="",size=15)
CTplot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在X轴上绘制分类数据时,您是在整数值x = 1,x = 2等上“真正”绘制的,但是使用文本标签代替数字。这就是使您可以将文本注释放在x = 1和x = 2处的文本注释。
但是,由于您已经在右侧添加了两个空的文本注释,因此栏束在左侧(位置x = 3和x = 4) 。该地块已扩大了适应它们的权利。由于它们是空的,因此您不需要它们。这是没有它们的情节:
,它是x = 4的空注释:
要强调这一点:
如您所见,X轴必须扩展以适应X = 20的无形文本注释。
如果您希望栏稍高一点,则可以执行以下操作:
< a href =“ https://i.sstatic.net/zyduv.png” rel =“ nofollow noreferrer”>
When you plot categorical data on the x axis, you are "really" plotting at integer values x = 1, x = 2, etc, but with the text labels used in place of numbers. This is what allows you to put text annotations at x = 1 and x = 2.
However, the bars are bunched at the left because you have added two empty text annotations over to the right (at position x = 3 and x = 4). The plot has expanded right to accommodate them. Since they are empty anyway, you don't need them. Here is the plot without them:
And here it is with an empty annotation at x = 4:
To emphasise the point, let's see an empty annotation at x = 20:
As you can see, the x axis has had to expand to accommodate the invisible text annotation at x = 20.
If you want the bars a bit more spread out, you can do something like:
Data used (approximated from image in OP)