R:水平条形图标签

时间:2019-12-07 15:23:37

标签: r bar-chart

我需要有关R中条形图的帮助。我没有使用搜索功能完全找到所需的内容。

我必须在一项实证研究中比较欧洲的失业率。我设法使基本结构正确。不幸的是,我没有设法在水平线后面得到相应的值。我已经附上了一张最终的样子的图片(红色值)

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)

我的数据:

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

也许您也有个技巧,告诉我如何更优雅地解决颜色问题。但这不是那么重要:-D

我不知道它是否相关,但是我在macOS Catalina上使用R-Studio。非常感谢!(对不起,我根本不是专家!:))

1 个答案:

答案 0 :(得分:0)

我只能为您的5个值尝试使用它,但它应该适用于您的所有数据。

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

我们设置颜色,如果欧罗巴(Europa)将其设为蓝色,如果德国深粉红色

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

我需要设置par()来查看标签,但是如果可以看到标签,则将其忽略。关键是保存条形图

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