使用GGPLOT2离散量表在X轴上进行间距

发布于 2025-02-06 10:43:00 字数 954 浏览 2 评论 0原文

我已经使用此代码了一段时间,但这是我第一次在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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2025-02-13 10:43:00

当您在X轴上绘制分类数据时,您是在整数值x = 1,x = 2等上“真正”绘制的,但是使用文本标签代替数字。这就是使您可以将文本注释放在x = 1和x = 2处的文本注释。

但是,由于您已经在右侧添加了两个空的文本注释,因此栏束在左侧(位置x = 3和x = 4) 。该地块已扩大了适应它们的权利。由于它们是空的,因此您不需要它们。这是没有它们的情节:

CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
  geom_col(fill = "lightsteelblue") + 
  geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
  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))+
  annotate("text",x=1,y=0.84,label="A",size = 5) +
  annotate("text",x=2,y=0.62,label="B",size = 5)

CTplot

”在此处输入图像描述“

,它是x = 4的空注释:

CTplot + annotate("text",x = 4, y = 0.29, label = "", size = 5) 

”

要强调这一点:

CTplot + annotate("text",x = 20, y = 0.29, label = "", size = 5) 

如您所见,X轴必须扩展以适应X = 20的无形文本注释。


如果您希望栏稍高一点,则可以执行以下操作:

CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
  geom_col(fill = "lightsteelblue", width = 0.6) + 
  geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
  scale_x_discrete(labels = xlabels, expand = c(0.75, 0)) +
  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))+
  annotate("text",x=1,y=0.84,label="A",size = 5) +
  annotate("text",x=2,y=0.62,label="B",size = 5)

CTplot

< a href =“ https://i.sstatic.net/zyduv.png” rel =“ nofollow noreferrer”>


CTsum <- data.frame(Treatment = c("A", "B"), FI = c(0.71, 0.48), meanse = 0.1)

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:

CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
  geom_col(fill = "lightsteelblue") + 
  geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
  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))+
  annotate("text",x=1,y=0.84,label="A",size = 5) +
  annotate("text",x=2,y=0.62,label="B",size = 5)

CTplot

enter image description here

And here it is with an empty annotation at x = 4:

CTplot + annotate("text",x = 4, y = 0.29, label = "", size = 5) 

enter image description here

To emphasise the point, let's see an empty annotation at x = 20:

CTplot + annotate("text",x = 20, y = 0.29, label = "", size = 5) 

enter image description here

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:

CTplot <- ggplot(CTsum, aes(Treatment, FI)) + 
  geom_col(fill = "lightsteelblue", width = 0.6) + 
  geom_errorbar(aes(ymin = FI - meanse, ymax = FI + meanse, width = 0.2)) +
  scale_x_discrete(labels = xlabels, expand = c(0.75, 0)) +
  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))+
  annotate("text",x=1,y=0.84,label="A",size = 5) +
  annotate("text",x=2,y=0.62,label="B",size = 5)

CTplot

enter image description here


Data used (approximated from image in OP)

CTsum <- data.frame(Treatment = c("A", "B"), FI = c(0.71, 0.48), meanse = 0.1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文