简体   繁体   English

如何修复 R 中 ggplot 上重叠的 y 轴 label?

[英]How to fix y-axis label overlapping on ggplot in R?

My y-axis for Age groups keep overlapping.我的年龄组 y 轴保持重叠。 Please help: This is my code:请帮助:这是我的代码:

ggplot(data = BRFSS2015_outlier1, aes(x = INCOME2, y= Age, color = SEX))+
  geom_col(width=0.03)+
  facet_grid(.~SEX)+
  labs(x = "Age")+
  theme(axis.text.x = element_text(angle = 30))

The output looks like this: output 看起来像这样: 在此处输入图像描述

It will help to see a sample of data using dput(BRFSS2015_outlier1) or if that's too long dput(head(BRFSS2015_outlier1)).使用 dput(BRFSS2015_outlier1) 或太长的 dput(head(BRFSS2015_outlier1)) 查看数据样本会有所帮助。

My guess is that Age is stored as a factor, and you haven't summarized, so each column is stacking many observations, resulting in tall columns that are way out of scale with the limited number of Age factors that are displayed in the y axis.我的猜测是 Age 被存储为一个因素,而你没有总结,所以每一列都堆叠了许多观察结果,导致高大的列与 y 轴上显示的有限数量的 Age 因素不成比例. You might, for instance, have an Age "30" which might be the first factor, appearing at "1" on the y axis, and Age "40" as the 2nd factor appearing at "2" on the y axis, but those are basically 0 compared to your stack that is hundreds or thousands of observations high.例如,您可能有一个年龄“30”,它可能是第一个因素,出现在 y 轴上的“1”处,年龄“40”作为第二个因素出现在 y 轴上的“2”处,但是那些与数百或数千个观察值高的堆栈相比,基本上为 0。

Here's a reproducible example using data we all have already.这是一个使用我们都已经拥有的数据的可重现示例。 Note that the y axis labels are all overlapped on the bottom like in your example.请注意,在您的示例中,y 轴标签全部重叠在底部。

ggplot(data = diamonds, aes(x = cut, y= as.factor(cut), color = clarity))+
  geom_col(width=0.03)+
  facet_grid(.~color)+
  theme(axis.text.x = element_text(angle = 30))

在此处输入图像描述

It's unclear to me what you want to show with Age on the y axis.我不清楚你想在 y 轴上用 Age 显示什么。 Is each bar supposed to be "as tall" as the Age?每个酒吧都应该与时代“一样高”吗? Or do you want to show how many observations there are of each age?或者您想显示每个年龄段有多少观察结果?

I'm guessing you wanted to show how the income distribution varies with Age, in which case it might be more appropriate to use Age as the vertical faceting variable, like this (replace "cut" here with Age in your example).我猜您想展示收入分配如何随年龄变化,在这种情况下,使用年龄作为垂直分面变量可能更合适,就像这样(在您的示例中将此处的“剪切”替换为年龄)。 Note also the use of geom_bar (which by default counts and displays the number of observations) instead of geom_col (which by default shows the raw value).另请注意使用geom_bar (默认情况下计算并显示观察次数)而不是geom_col (默认情况下显示原始值)。

ggplot(data = diamonds, aes(x = cut, fill = cut))+
  geom_bar()+
  facet_grid(color~clarity)+
  theme(axis.text.x = element_text(angle = 30))

在此处输入图像描述

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

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