简体   繁体   中英

How to make bar charts shorter with ggplot?

I basically have a very simple question, how can I make bar charts shorter? If I search for it on the web I can only find "width", but this is not what I want. I want to make the lenght of the bars shorter.


library(ggplot2)

# data
data <- data.frame(
  name=c("A","B","C","D","E") ,
  value=c(3,12,5,18,45)
)

# bar chart
ggplot(data, aes(x=name, y=value)) +
  geom_bar(stat = "identity", width=0.8) +
  coord_flip()

I basically just want that the lenght of the bar goes up to like around 20, so somehow just half the length..

I think you mean how do you make the bars visibly shorter on the page without affecting their numerical representation. Perhaps you just need to set the axis limits?

If your plot looks like this...

p <- ggplot(data, aes(x=name, y=value)) +
  geom_bar(stat = "identity", width=0.8) +
  coord_flip()
p

Then you can just do this...

p + scale_y_continuous(limits = c(0, 2 * max(data$value)))

Created on 2020-11-10 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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