1

I am trying to figure out how to put the labels inside a horizontal bar plot. I am using ggplot2 library. I set the parameters in geom_text but do not know which is the right one.

library(ggplot2)

ggplot(below_mean_above_eta_pos_synergy, aes(x=enrollments, y=course_code, fill = type)) + 
  geom_bar(stat = "identity") +
  ggtitle("Below average courses, high interaction rate, positive synergy") +
  xlab("Enrollments") + ylab("Courses")+guides(fill=guide_legend(title="Course Category")) +theme(plot.title = element_text(face="bold")) +
  geom_text(aes(label=enrollments), vjust=0.3, position = position_dodge(0.9), size = 3) +theme(axis.text.x = element_text())

Code:

> dput(below_mean_above_eta_pos_synergy)
structure(list(X = c(1L, 2L, 3L, 4L, 7L, 9L, 12L, 13L, 17L, 18L, 
19L, 21L, 36L, 37L, 39L, 44L, 48L, 51L, 55L, 58L, 64L, 78L, 79L, 
80L), course_code = c("imdb2012", "bpm2013", "imdb2013", "internetworking2013", 
"www2013", "internetworking2014", "semanticweb2014", "imdb2015", 
"semanticweb2015", "softwareanalytics2015", "webtech2015", "bpm2016", 
"insights-2017", "javaeinstieg2017", "mainframes2017", "webtech2017", 
"intsec2018", "javacapstone-2018", "ruby2018", "designthinking2019", 
"international-teams2019", "knowledgegraphs2020", "learningtheory2020", 
"neuralnets2020"), Language = c("English", "English", "English", 
"German", "German", "English", "English", "English", "English", 
"English", "English", "English", "English", "German", "German", 
"German", "English", "English", "German", "English", "English", 
"English", "English", "German"), Year = c(2012L, 2013L, 2013L, 
2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2016L, 
2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2019L, 2019L, 
2020L, 2020L, 2020L), completion_rate = c(0.1816, 0.2291, 0.2441, 
0.1746, 0.2098, 0.2987, 0.1511, 0.2231, 0.3124, 0.2681, 0.1583, 
0.2747, 0.2585, 0.3213, 0.2622, 0.2302, 0.2237, 0.0375, 0.1643, 
0.2355, 0.2061, 0.1563, 0.0489, 0.2388), synergy_scores = c(0.1291707, 
0.1069797, 0.1046794, 0.08481919, 0.08007633, 0.0803255, 0.09768569, 
0.1095156, 0.1012467, 0.08715165, 0.07935962, 0.09150103, 0.09904272, 
0.0713112, 0.07845346, 0.08701674, 0.06314873, 0.08876871, 0.0627783, 
0.2457304, 0.1290873, 0.07539301, 0.02656829, 0.08053228), eta = c(0.97, 
0.93, 0.8, 0.91, 0.97, 0.88, 0.78, 0.89, 0.85, 0.87, 0.95, 0.95, 
0.84, 0.97, 0.83, 0.96, 0.88, 0.89, 0.95, 0.85, 0.9, 0.87, 0.92, 
0.97), type = c("Database", "Business", "Database", "Web", "Web", 
"Web", "Web", "Database", "Web", "Programming", "Web", "Business", 
"Design Thinking", "Programming", "Cloud", "Web", "Security", 
"Programming", "Programming", "Design Thinking", "General", "General", 
"AI", "Programming"), enrollments = c(18631L, 16685L, 19319L, 
16068L, 13535L, 11704L, 7428L, 14185L, 11166L, 6027L, 15262L, 
12553L, 10110L, 17827L, 5334L, 13161L, 12867L, 4087L, 8268L, 
6250L, 5319L, 8918L, 3608L, 12303L)), row.names = c(NA, -24L), class = "data.frame")

enter image description here

How do I put these labels inside the bars?

Ranji Raj
  • 778
  • 4
  • 18

2 Answers2

2

To get the text in the middle of the bars, use position = position_stack(vjust = 0.5) inside geom_bar.

Incidentally, geom_bar(stat = "identity") is just a long way of writing geom_col()

library(ggplot2)

ggplot(below_mean_above_eta_pos_synergy, 
       aes(x = enrollments, y = course_code, fill = type)) + 
  geom_col() +
  geom_text(aes(label = enrollments),
            position = position_stack(vjust = 0.5), size = 3) +
  labs(title = "Below average courses, high interaction rate, positive synergy",
       x = "Enrollments", y = "Courses", fill = "Course Category") +
  theme(plot.title = element_text(face = "bold")) 

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
2

Another options just to add hjust=1.1 to your code:

library(ggplot2)

ggplot(below_mean_above_eta_pos_synergy, aes(x=enrollments, y=course_code, fill = type)) + 
  geom_bar(stat = "identity") +
  ggtitle("Below average courses, high interaction rate, positive synergy") +
  xlab("Enrollments") + ylab("Courses")+guides(fill=guide_legend(title="Course Category")) +theme(plot.title = element_text(face="bold")) +
  geom_text(aes(label=enrollments), vjust=0.3, position = position_dodge(0.9), hjust = 1.1, size = 3) +theme(axis.text.x = element_text())

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66