Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
890 views
in Technique[技术] by (71.8m points)

r - ggplot2: how to reduce space between narrow width bars, after coord_flip, and panel border

When you have flipped coordinates, how do you reduce the space between bars that are narrow and the panel border? Using the data frame df and the ggplot commands, there is much white space between the bottom bar and the tick marks (and similarly a wide space above the "vendor" bar).

df <- data.frame(x = c("firm", "vendor"), y = c(50, 20))

ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity", width = 0.4) + 
  theme_tufte() +  coord_flip() +
  labs(x = "", y = "")

enter image description here

I tried scale_x_discrete with both limits and expand arguments to no avail as well as position = position dodge, likewise with no effect.

This question offers coord_equal to change the aspect ratio, and thereby reduce or eliminate the extra space, but notes that the solution does not work with coord_flip.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I have found a solution. You can remove width from geom_bar and introduce theme(aspect.ratio = .2), then you can play with the ratio to find the desired width. And unlike coord_equal or coord_fixed is compatible with coord_flip.

ggplot(df, aes(x = x, y = y)) + 
  geom_bar(stat = "identity") + 
  theme_tufte() + theme(aspect.ratio = .2) +
  coord_flip() +
  labs(x = "", y = "")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...