简体   繁体   中英

how to plot negative numbers from small to large value using geom_bar in ggplot2

I would like to be able to plot a bar chart from left to right using only negative values. The low value is on the left with the bar length reaching to the single observed value. If I need to give a starting minimum value, I can do that, say -105 in the example I provide below.

for some reason I am not able to use xlim or ylim to control the scale

a <- as.factor(c("x","y","z"))
b <- c(-105, -87, -99)
neg_tib <- tibble(a,b)

neg_plot <- ggplot(neg_tib, aes(x = a, y = b))+
  geom_bar(stat = "identity",
           position = "identity", 
           color = "blue", fill = "transparent", 
           width = .1) +
  scale_y_continuous(breaks = c(-80, -100)) +
  coord_flip() +
  theme_tufte() 

在此处输入图像描述

You can use scale_y_reverse() instead of scale_y_continuous() to reverse your axis and get something that reverses the bars. I also added breaks every -20 units to make it look a bit more complete:

neg_plot <- ggplot(neg_tib, aes(x = a, y = b))+
  geom_bar(stat = "identity",
           position = "identity", 
           color = "blue", fill = "transparent", 
           width = .1) +
  scale_y_reverse(breaks = seq(0,-100, by=-20)) +
  coord_flip() +
  theme_tufte() 

在此处输入图像描述

Setting axis limits on bar plots is heavily discouraged, as it is convention that bars "start" at 0. If you are looking to show where you set limits on the axis, I would suggest using another geom (points, lines, etc... depends on your data).

Nevertheless, if you absolutely must "zoom in" on the edges of bars, you can set the limits within coord_flip() . Why there? You can set limits in scale_y_reverse(limits=c(...)) ; however, just like other scale_* functions, the limits within these functions and via xlim() and ylim() end up zooming in on your data and removing any datapoints outside those limits . This is not always a problem, but for the bar and col geoms, for example, these actually start at 0 and extend to whatever the y aesthetic would be. So for the bar that ends at -105, the geom extends from 0 to -105. This means when you zoom in with limits= inside of scale_y_reverse() or ylim() , it will zoom the axis, but it will no longer plot those geoms (because they extend outside those limits).

To zoom in but keep all geoms regardless of whether or not they extend outside the limits set, you can set those limits inside any coord_* function. That includes coord_cartesian() , or in your case, coord_flip() . Here's what it looks like zoomed in. I've also set the breaks on your y axis to be smaller to accomodate the different zoom:

neg_plot <- ggplot(neg_tib, aes(x = a, y = b))+
  geom_bar(stat = "identity",
           position = "identity", 
           color = "blue", fill = "transparent", 
           width = .1) +
  scale_y_reverse(breaks = seq(0,-100, by=-20)) +
  coord_flip() +
  theme_tufte() 

在此处输入图像描述

Once again, I have to emphasize it is really bad practice to show bar graphs this way . The reason is that in the plot above it looks like z is about double the value of y , while in reality the value of z is -99 and the value of y is -87. Definitely not double that of y , unless you "start" at -80.

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