0

I already searched for an answer but I'm not able to fix my problem with labelling a barplot in R. So I want to display the frequencies at the top of the bars and this works, but the position differs between them. Of course I want to display them in the middle

Barplot

png ("Studiengang.png", width = 600, height = 500) 
par(mar = c (8, 5, 2, 2)) 
barplot (table$SG_A[1:7], names = table$SG[1:7], ylim = c (0, 50), las=2, ylab = "Anteil [%]")    
text(table$SG_A[1:7], labels=table$SG_A[1:7], pos = 3, cex = 0.8 , col = "black") 
box() 
dev.off()

What do I need to change? I really appreciate help!

dput(table)
structure(list(GES = structure(c(4L, 3L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("", "k.A / Sonst.", "männlich", 
"weiblich"), class = "factor"), GES_A = c(89.66, 8.05, 2.3, NA, 
NA, NA, NA, NA, NA, NA, NA, NA), SG = structure(c(3L, 2L, 6L, 
4L, 7L, 8L, 5L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Erziehungsw.", 
"Lehramt", "Psychologie", "Sonstige", "SOP", "UNN", "UWS"), class = "factor"), 
    SG_A = c(41.24, 21.65, 10.31, 3.09, 2.06, 2.06, 9.28, NA, 
    NA, NA, NA, NA), SEM = 1:12, SEM_A = c(3.45, 24.14, 4.6, 
    34.48, 2.3, 17.24, 3.45, 5.75, 0, 2.3, 1.15, 1.15), WOHN = structure(c(7L, 
    5L, 3L, 2L, 8L, 4L, 6L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "Alleine", "Eltern", "Familie", "Partner", "Sonstige", "WG", 
    "Wohnheim"), class = "factor"), WOHN_A = c(39.08, 22.99, 
    13.79, 8.05, 8.05, 6.9, 1.15, NA, NA, NA, NA, NA), KLASSE_B = structure(c(6L, 
    7L, 2L, 3L, 4L, 5L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "gelegentlich", "häufig", "immer", "k.A.", "nie", "selten"
    ), class = "factor"), MEHRWEG = c(43.68, 6.9, 4.6, 3.45, 
    4.6, 36.78, NA, NA, NA, NA, NA, NA), PFAND = c(42.53, 5.75, 
    3.45, 4.6, 3.45, 40.23, NA, NA, NA, NA, NA, NA), EINWEG = c(21.84, 
    6.9, 3.45, 13.79, 14.94, 39.08, NA, NA, NA, NA, NA, NA), 
    KLASSE_M = structure(c(5L, 6L, 2L, 3L, 4L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L), .Label = c("", "gelegentlich", "häufig", "immer", 
    "nie", "selten"), class = "factor"), FUSS = c(27.59, 24.14, 
    16.09, 17.24, 14.94, NA, NA, NA, NA, NA, NA, NA), RAD = c(43.68, 
    13.79, 17.24, 20.69, 4.6, NA, NA, NA, NA, NA, NA, NA), OEFF = c(16.09, 
    14.94, 18.39, 33.33, 17.24, NA, NA, NA, NA, NA, NA, NA), 
    AUTO = c(40.23, 28.74, 12.64, 11.49, 6.9, NA, NA, NA, NA, 
    NA, NA, NA), MUELLTRENN = structure(c(2L, 3L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "ja", "nein"), class = "factor"), 
    MUELLTRENN_ZU = c(97.7, 2.3, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA), MUELLTRENN_UNI = c(67.82, 32.18, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA), Z_GELB = c(96.47, 3.53, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA), Z_REST = c(95.29, 4.71, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Z_BIO = c(72.94, 
    27.06, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Z_PAPIER = c(1L, 
    0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Z_GLAS = c(92.94, 
    7.06, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Z_SONST = c(10.59, 
    89.41, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), ZU_CAM = c(97.7, 
    2.3, 67.82, 32.18, NA, NA, NA, NA, NA, NA, NA, NA), MUELLTRENN_G = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("ja", 
    "nein"), class = "factor"), ZUHAUSE = c(95.29, 4.71, 72.94, 
    27.06, 100, 0, 96.47, 3.53, 92.94, 7.06, 10.59, 89.41), C_GELB = c(70L, 
    30L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), C_REST = c(93.33, 
    6.67, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), C_BIO = c(40L, 
    60L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), C_PAPIER = c(83.33, 
    16.67, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), C_GLAS = c(26.67, 
    73.33, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), C_SONST = c(1.67, 
    98.33, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), CAMPUS = c(93.33, 
    6.67, 40, 60, 83.33, 16.67, 70, 30, 26.67, 73.33, 1.67, 98.33
    )), class = "data.frame", row.names = c(NA, -12L))
Saskia
  • 23
  • 4
  • [this](https://stackoverflow.com/questions/30656846/draw-the-sum-value-above-the-stacked-bar-in-ggplot2) might help. – Prem Jun 28 '18 at 13:04
  • yes, thank you. Thats ggplot. But I already wrote my skript for the normal barplot function in r. So if possible I don't want to change everything to ggplot – Saskia Jun 28 '18 at 13:21
  • 1
    You may need to share sample data using `dput()` to [make your plot reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Prem Jun 28 '18 at 13:27
  • ok, thank you! I added the returned data of the dput() function above – Saskia Jun 28 '18 at 13:47
  • When possible please provide a simplified [reproducible example](https://stackoverflow.com/help/mcve). This giant block of code is hard for everyone to look at, in order to help you better create a similar example but use a well know data set like `faithful` or `mtcars` and show the code to format it like your data. –  Jun 28 '18 at 14:54

1 Answers1

1

Here is one possible solution:

barplotData <- c(barplot(table$SG_A[1:7], names = table$SG[1:7]) ) # saves x positions

You need to specify how much space should be above the height of bars (y = 2 + SG_A)

par(mar = c (8, 5, 2, 2)) 
barplot (table$SG_A[1:7], names = table$SG[1:7], ylim = c (0, 50), las=2, ylab = "Anteil [%]")    
text(x=barplotData, y= 2 + table$SG_A[1:7], labels=table$SG_A[1:7])
box()
Lstat
  • 1,450
  • 1
  • 12
  • 18
  • 1
    Glad to hear it helped :) Please consider accepting the answer so we can close this question. – Lstat Jun 29 '18 at 15:23