1

I need help with a bar chart in R. I did not find exactly what I was looking for using the search function.

I have to compare the European unemployment rate in an empirical work. I managed to get the basic structure right. Unfortunately, I didn't manage to get the corresponding values behind the horizontal bars. I have attached a picture of what it should look like in the end (red values)

options(stringsAsFactors = F)
ewq<- read.table(file='Europa.txt', header=TRUE, sep = ';')
attach(ewq)
par(mar=c(5,10,2,2))
barplot(Wert3, names.arg = Land,
    horiz=T,
    main= "Erwerbslosenquote in ausgewählten EU-Ländern",
    cex.names=0.8,
    xlim=c(0,20),
    ylim=c(0,19),
    col= c("honeydew3", "honeydew3","honeydew3","honeydew3","azure","honeydew3",
            "honeydew3","honeydew3","honeydew3","honeydew3","honeydew3","honeydew3",
            "honeydew3","honeydew3","deeppink","honeydew3"),
    xlab= "Erwerbslosenquote in Prozent [Stand: Okt. 2019]",
    las=1)

My Data:

Land;Wert3
Griechenland (EU-Max);16.7
Spanien;14.2
Italien;9.7
Frankreich;8.5
Europa;6.8
Schweden;6.8
Portugal;6.5
Litauen;6.4
Irland;4.8
Österreich;4.6
Rumänien;4
Vereinigtes Königreich;3.8
Niederlande;3.5
Polen;3.2
Deutschland;3.1
Tschechien (EU-Min);2.2

This is what it should look like

This is my dataframe

Maybe you also have a tip how I could solve the thing with the colors more elegantly. But it's not that important :-D

I don't know if it's relevant, but I use R-Studio on the macOS Catalina. Thanks a lot!( Sorry, I'm not an expert at all!:) )

  • 2
    Similar question has already been answered, check here: https://stackoverflow.com/questions/12481430/how-to-display-the-frequency-at-the-top-of-each-factor-in-a-barplot-in-r. Otherwise try `text(x = Wert3, y = Land, label = Values, pos = val, cex = val, col = "red".` you can change them according to your dataframe. I cannot do anything unless given a reproducible code, check here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Lime Dec 07 '19 at 16:44
  • Thank you Meilton! i added my full code and shared my data.txt. Unfortunately your code doesn't work for me. I get a Error. `Object 'val' not found` – Tropical_economic Dec 07 '19 at 17:22
  • Apologies, I should be been more specific. 'val' was an indicator for a value of your choice to play around with, this would be any integer, such that `pos = 2`, `cex = 0.3`. Hope that helps. – Lime Dec 07 '19 at 17:29
  • There's no need to apologize. I'm just an ultimate beginner in R. I've got some kind of manual from my university, but I just can't deal with it. The manual is simply much too complex. Thank you! – Tropical_economic Dec 07 '19 at 17:39

1 Answers1

0

I can only try it for 5 of your values, but it should work for all your data.

Land<-c("Spanien", "Italien", "Europa", "Deutschland", "Polen")
Wert3<-c(14.2, 9.7, 6.8, 3.2, 3.1)

We set the colors, if Europa make it azure, if Deutschland deeppink

COLS = rep("honeydew3",length(Land))
COLS[Land=="Europa"] = "azure"
COLS[Land=="Deutschland"] = "deeppink"

I need to set the par() to see the labels, but ignore it if you can see the labels. Key is to save the barplot

par(mar=c(5,5,5,5))
BAR = barplot(Wert3, names.arg = Land,
    horiz=T,
    main= "Erwerbslosenquote in ausgewählten EU-Ländern",
    cex.names=0.8,
    xlim=c(0,20),
    ylim=c(0,6),
    col= COLS,
    xlab= "Erwerbslosenquote in Prozent [Stand: Okt. 2019]",
    las=1)

text(x=Wert3+2,y=BAR,Wert3,col="red",cex=0.7)

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72