简体   繁体   English

具有所有 x 轴值的 ggplot x 轴标签

[英]ggplot x-axis labels with all x-axis values

I'm plotting ggplot with geom_point .我正在用geom_point ggplot The x-axis will be individuals' ID, and y-axis is variable A. How can I ggplot all and individual ID values on the x-axis without overlapping labels? x 轴将是个人 ID,y 轴是变量 A。如何在 x 轴上 ggplot 所有和个人 ID 值而不重叠标签? ID may not be continuous. ID 可能不连续。

df sample (actual rows are much longer) df 样本(实际行长得多)

> df
ID     A
1      4
2      12
3      45
5      1

Code for the plot:情节代码:

ggplot(df, aes(x = ID, y = A)) + geom_point()

Above code has x-axis in intervals, but not presenting individual ID.上面的代码有间隔的 x 轴,但不显示单独的 ID。

Thanks!谢谢!

Is this what you're looking for? 这是你在找什么?

ID <- 1:50
A <- runif(50,1,100)

df <- data.frame(ID,A)

ggplot(df, aes(x = ID, y = A)) + 
  geom_point() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  scale_x_continuous("ID", labels = as.character(ID), breaks = ID)

This will produce this image: 这将产生这个图像:

在此输入图像描述

So you'll get a label for every ID-value. 因此,您将获得每个ID值的标签。 If you'd like to remove the gridlines (There are too much for my taste) you can remove them by adding theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 如果你想删除网格线(有太多我的口味)你可以通过添加theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())删除它们

EDIT: The easier way would be to just use ID as a factor for the plot. 编辑:更简单的方法是使用ID作为情节的一个因素。 like this: 像这样:

ggplot(df, aes(x = factor(ID), y = A)) + 
  geom_point() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  xlab("ID")

在此输入图像描述

The advantage of this method is that you don't get empty spaces from missing IDs 此方法的优点是您不会从缺少的ID中获取空白空间

EDIT2: Concerning your Problem with overlapping labels: I'm guessing it comes from a large number of IDs to be plotted. 编辑2:关于重叠标签的问题:我猜测它来自大量要绘制的ID。 There are several ways we can deal with this. 我们有几种方法可以解决这个问题。 So lets say your plot looks like this: 所以我们说你的情节看起来像这样:

在此输入图像描述

One idea would be to hide every 3rd label from the x-axis by modifying the break argument of the axis: 一种想法是通过修改轴的break参数来隐藏x轴上的每个第3个标签:

ggplot(df, aes(x = factor(ID), y = A)) + 
  geom_point() + 
  scale_x_discrete(breaks = ID[c(T,F,F)]) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  xlab("ID")

which leads to this: 这导致了这个:

在此输入图像描述

If hiding labels is not an option, you could split your plot into subplots. 如果无法隐藏标签,则可以将绘图拆分为子图。

df$group <- as.numeric(cut(df$ID, 4))

ggplot(df, aes(x = factor(ID), y = A)) + 
  geom_point() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  xlab("ID") +
  facet_wrap(~group, ncol = 1, scales = "free_x")

which leads to this: 这导致了这个:

在此输入图像描述

Just add + xlim()<\/code> and + ylim()<\/code> to show the full x axis and y axis (ie to make the x axis and y axis start at zero).只需添加+ xlim()<\/code>和+ ylim()<\/code>即可显示完整的 x 轴和 y 轴(即使 x 轴和 y 轴从零开始)。

Reproducible example可重现的例子<\/h3>

If this is your ggplot:如果这是你的 ggplot:

 iris %>% ggplot(aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()<\/code><\/pre>

在此处输入图像描述<\/a>

simply add these two lines to make the x and y axes start at zero<\/strong> :只需添加这两行,使 x 和 y 轴从零开始<\/strong>:

 iris %>% ggplot(aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + xlim(0, 8.2) + # add this line for x axis limits ylim(0, 4.5) # add this line for y axis limits<\/code><\/pre>

在此处输入图像描述<\/a>

"

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

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