简体   繁体   English

使用 ggplot2 离散刻度在 x 轴上的间距

[英]Spacing on x-axis using ggplot2 discrete scale

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.我使用此代码已有一段时间,但这是我第一次在 x 轴上只有两个类别。 For some reason, R will not space out the bins and instead bunches them at the beginning.出于某种原因,R 不会将垃圾箱隔开,而是在开始时将它们聚在一起。 How do I evenly spread them along the x-axis?如何沿 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

在此处输入图像描述

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.当您在 x 轴上使用 plot 分类数据时,您实际上是在绘制 integer 值 x = 1、x = 2 等,但使用文本标签代替数字。 This is what allows you to put text annotations at x = 1 and x = 2.这就是允许您在 x = 1 和 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).但是,条形图在左侧聚集,因为您在右侧添加了两个空文本注释(在 position x = 3 和 x = 4)。 The plot has expanded right to accommodate them. plot 已扩展为容纳它们。 Since they are empty anyway, you don't need them.由于它们无论如何都是空的,因此您不需要它们。 Here is the plot without them:这是没有它们的 plot:

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

在此处输入图像描述

And here it is with an empty annotation at x = 4:这里它在 x = 4 处有一个空注释:

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

在此处输入图像描述

To emphasise the point, let's see an empty annotation at x = 20:为了强调这一点,让我们在 x = 20 处看一个空注释:

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

在此处输入图像描述

As you can see, the x axis has had to expand to accommodate the invisible text annotation at x = 20.如您所见,x 轴必须扩展以容纳 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

在此处输入图像描述


Data used (approximated from image in OP)使用的数据(从 OP 中的图像近似)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM