如何用长标签维护ggplot的大小 [英] How to maintain size of ggplot with long labels

查看:290
本文介绍了如何用长标签维护ggplot的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情节,是一个简单的每个事件类型的数字的barplot。我需要剧情的标签在情节之下,因为一些事件有很长的名字,并且横向压扁剧情。我试图移动标签下的标签,但现在在有很多事件类型时会被压扁。有没有一种方法可以得到一个静态阴谋的大小(即条形图),以便长传说不会压制阴谋?我的代码:

  ggplot(counts_df,aes(x = Var2,y = value,fill  -  Var1)+ 
geom_bar(stat =identity )+
主题(legend.position =bottom)+
主题(legen.direction =vertical)+
主题(axis.text.x = element_text(angle = -90) )

结果:



我认为这是因为图像大小必须是静态的,所以图形会被牺牲。我把一个传说放在阴谋之下。

解决方案

有几种方法可以避免标签重叠绘图或挤压绘图区域或改善一般的可读性。建议中的哪一个d解决方案是最合适的将取决于标签的长度和条数以及其他一些因素。所以,你可能不得不玩耍。



虚拟数据



不幸的是,OP没有包括在内一个可重复的例子,所以我们必须补充我们自己的数据:

  V1 <-c(Long label, 更长的标签,更长的标签,
非常非常长的标签,非常长的标签,
所有可能标签的长,更长,最长的标签 b $ b另一个标签,短,不是很短的标签)
df < - data.frame(V1,V2 = nchar(V1))
yaxis_label < - A字符计数的长轴标签



标准条形图



x轴上的标签直立打印,彼此重叠:

  library(ggplot2)#版本2.2.0 + 
p < - ggplot(df,aes(V1,V2))+ geom_col()+ xlab(NULL)+
ylab(yaxis_label)
p

请注意,最近添加的 geom_col() code> geom_bar(stat =identity)正在使用中。



水平条形图



所有标签(包括y轴标签)均直立打印,提高了可读性,但仍然挤压了绘图区域(但在较小程度上如图表所示以横向格式):

  p + coord_flip()



带标签的垂直条形图包装



标签直立打印,避免出现重叠绘图,减少了绘图区域的挤压。您可能必须将 width 参数放在 stringr :: str_wrap 中。

  q <-p + aes(stringr :: str_wrap(V1,15),V2)+ xlab(NULL)+ 
ylab(yaxis_label)
q



带标签的水平条形图包装



我最喜欢的方法是:所有标签都直立打印,提高可读性,
挤压小区面积减少。同样,你可能需要使用 width 参数来 stringr :: str_wrap 来控制行数标签分成。

  q + coord_flip()



附录:使用 scale_x_discrete()



为了完整起见,应该提到 ggplot2 能够缩写标签。

  p + scale_x_discrete(labels = abbreviate)

< img src =https://i.stack.imgur.com/uq7Je.pngalt =在这里输入图片描述>


I have a plot that is a simple barplot of number of each type of an event. I need the labels of the plot to be under the plot as some of the events have very long names and were squashing the plot sideways. I tried to move the labels underneath the plot but it now gets squashed upwards when there are lots of event types. Is there a way of having a static plot size (i.e. for the bar graph) so that long legends don't squash the plot?

My code:

ggplot(counts_df, aes(x = Var2, y = value, fill - Var1)+
    geom_bar(stat = "identity") +
    theme(legend.position = "bottom") +
    theme(legen.direction = "vertical") +
    theme(axis.text.x = element_text(angle = -90)

The result:

I think this is because the image size must be static so the plot gets sacrificed for the axis. The same thing happens when I put a legend beneath the plot.

解决方案

There a several ways to avoid overplotting of labels or squeezing the plot area or to improve readability in general. Which of the proposed solutions is most suitable will depend on the lengths of the labels and the number of bars, and a number of other factors. So, you will probably have to play around.

Dummy data

Unfortunately, the OP hasn't included a reproducible example, so we we have to make up our own data:

V1 <- c("Long label", "Longer label", "An even longer label",
        "A very, very long label", "An extremely long label",
        "Long, longer, longest label of all possible labels", 
        "Another label", "Short", "Not so short label")
df <- data.frame(V1, V2 = nchar(V1))
yaxis_label <- "A rather long axis label of character counts"

"Standard" bar chart

Labels on the x-axis are printed upright, overplotting each other:

library(ggplot2)  # version 2.2.0+
p <- ggplot(df, aes(V1, V2)) + geom_col() + xlab(NULL) +
  ylab(yaxis_label) 
p

Note that the recently added geom_col() instead of geom_bar(stat="identity") is being used.

OP's approach: rotate labels

Labels on x-axis are rotated by 90° degrees, squeezing the plot area:

p + theme(axis.text.x = element_text(angle = 90))

Horizontal bar chart

All labels (including the y-axis label) are printed upright, improving readability but still squeezing the plot area (but to a lesser extent as the chart is in landscape format):

p + coord_flip()

Vertical bar chart with labels wrapped

Labels are printed upright, avoiding overplotting, squeezing of plot area is reduced. You may have to play around with the width parameter to stringr::str_wrap.

q <- p + aes(stringr::str_wrap(V1, 15), V2) + xlab(NULL) +
  ylab(yaxis_label)
q

Horizontal bar chart with labels wrapped

My favorite approach: All labels are printed upright, improving readability, squeezing of plot area are is reduced. Again, you may have to play around with the width parameter to stringr::str_wrap to control the number of lines the labels are split into.

q + coord_flip()

Addendum: Abbreviate labels using scale_x_discrete()

For the sake of completeness, it should be mentioned that ggplot2 is able to abbreviate labels. In this case, I find the result disappointing.

p + scale_x_discrete(labels = abbreviate)

这篇关于如何用长标签维护ggplot的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆