R - 占用相同空间的 ggplot 标签



Rplot值标签以我们无法读取图形的方式占用相同的空间。我该如何解决这个问题?我能想到的唯一方法是更改变量级别名称本身,但是数据集很大,我也不知道该怎么做。 有没有办法更改轴中的标签?

ggplot(data = brfss2013, aes(x = X_smoker3, y = educa), bins = 10) + labs(x = "Computed Smoking Status", y = "Education level") + geom_bar(stat='identity', col = "darkorange", fill = "darkorange")

我不知道数据细节,但在我看来,X_smoker3educa都是分类变量,您的绘图试图绘制由这两个分类变量的不同水平定义的每个子组中的计数。

我建议你用颜色来表示其中一个分类变量的不同级别(在这里说educa(。然后你绘制的X_smoker3y_axis,countx_axis,不同的颜色代表不同的educa级别。

我没有您的数据,但我使用 ggplot2 包中的数据集diamonds作为示例来展示解决方案。这里的claritycolor都是分类变量,在我翻转坐标后,clarity级别将是我的y_axis,count是x_axis,color级别表示为不同的颜色。

ggplot(data = diamonds, mapping = aes(x = clarity,fill = color))+
layer(geom = "bar",stat = "count",position = "identity")+
coord_flip()

生成的情节在这里

我们无法重现您的问题,因为您尚未提供数据集,但在ggplot3.3.0 或更高版本中,您可以避开标签。例如,您可以添加:

ggplot(data = diamonds, mapping = aes(x = clarity,fill = color))+
layer(geom = "bar",stat = "count",position = "identity")+
coord_flip() + scale_x_discrete(guide = guide_axis(n.dodge=3))

请参阅:https://datavizpyr.com/how-to-dodge-overlapping-text-on-x-axis-labels-in-ggplot2/

最新更新