Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r ggplot2 How to make vjust in geom_text put label at bottom of bar

Tags:

r

ggplot2

I am attempting to solve two problems:

  1. The first is to let some bars in a bar graph go beyond the top of a graph.
  2. The second is to insert the value of the bar at the bottom of the graph, so the person looking at the graph can tell just how far above the graph the bar goes.

The code below is my attempt to solve both of these. It solves the first with coord_cartesian, in the sense that a bar runs off the top of the graph. But it doesn't solve the second. The values of the bar are at the top of the bar rather than at the bottom. I thought vjust = "bottom" or 0 would do the trick, but I've tried it in a variety of places without success.

library(ggplot2)
library(data.table)

dt <- data.table(
  scenario = c("2010", "SSP2-NoCC", "SSP2-HGEM", "SSP1-NoCC", "SSP3-NoCC", "2010", "SSP2-NoCC", "SSP2-HGEM", "SSP1-NoCC", "SSP3-NoCC", "2010", "SSP2-NoCC", "SSP2-HGEM", "SSP1-NoCC", "SSP3-NoCC", "2010", "SSP2-NoCC", "SSP2-HGEM", "SSP1-NoCC", "SSP3-NoCC"),
  value = c(45.75, 15.74, 17.16, 10.73, 24.03, 15.37, 6.87, 7.61, 5.63, 8.87, 9, 3.43, 3.76, 2.93, 4.01, 2.53, 1.79, 1.95, 1.77, 1.79),
  region_name = c("Low", "Low", "Low", "Low", "Low", "Lower middle", "Lower middle", "Lower middle", "Lower middle", "Lower middle", "Upper middle", "Upper middle", "Upper middle", "Upper middle", "Upper middle", "High", "High", "High", "High", "High")
)

yLab <- "(percent)"
yRange <- c(0, 40)
plotTitle <-"Food Budget Share Of Per Capita Income"
colorList <- c("#000000", "#FEF0D9", "#2CA25F", "#FC8D59", "#D7301F")


ggplot(data = dt, aes(x = factor(region_name), y = value, group = scenario)) +
  geom_col(aes(fill = scenario), position = "dodge", color = "black") +
  coord_cartesian(ylim = yRange) +
  theme(legend.position = "none") +
  labs(x = NULL, y = yLab) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, family = "Times", face = "plain")) +
  theme(axis.title.y = element_text(family = "Times", face = "plain")) +
  geom_text(aes(label = value, vjust = "bottom"), position = position_dodge(0.9), size = 3, angle = 90,  color = "white") +
  scale_fill_manual(values = colorList) +
  theme(plot.title = element_text(hjust = 0.5, size = 11, family = "Times", face = "plain")) +
  ggtitle(plotTitle)

enter image description here

like image 678
JerryN Avatar asked Apr 02 '18 01:04

JerryN


People also ask

How do I label a bar value in R?

To add labels on top of each bar in Barplot in R we use the geom_text() function of the ggplot2 package. Parameters: value: value field of which labels have to display. nudge_y: distance shift in the vertical direction for the label.

How do I add labels in Ggplot?

To add labels at specified points use annotate() with annotate(geom = "text", ...) or annotate(geom = "label", ...) . To automatically position non-overlapping text labels see the ggrepel package.

Which aesthetic is used together with Geom_text?

Aesthetics shared with geom_text() . These include label , alpha , family , fontface and size . Additionally, the textcolour aesthetic can be used to set the text colour independent of the general colour.


1 Answers

The trick to your second problem is to map the y aesthetic to a fixed value. I used y = 1.25 but you can adjust as you please. I also used size = 2.25 because it looks better, in my opinion. And instead of vjust = 'bottom', use hjust = 'left' because you are applying these settings at a 90 degree rotation. So the geom_text portion should be

geom_text(data = dt,
          aes(x = factor(region_name), y = 0.5, label = round(value, 1)),
          position = position_dodge(0.9),
          size = 2.25, angle = 90,
          color = "white", hjust = 'left')

enter image description here

like image 196
hpesoj626 Avatar answered Oct 03 '22 11:10

hpesoj626