在R中的直方图中显示(值)标签

时间:2017-04-11 15:44:43

标签: r plot graphics histogram labels

我在R中有一个带有字符串值标签(占用组)的变量,但变量的实际值是数字。

我想将变量显示为直方图,它会显示附加到某些数字的标签 - 现在,我只能用数字值标记条形(使用" hist( occgroupsn,labels = TRUE)") Histogram with numbers

这是我添加值标签的方式(对于凌乱的代码而言:抱歉):

occgroups <- ordered(IscoGroups,
                 levels = c("01",
                            "02",
                            "03",
                            "11",
                            "12",
                            "13",
                            "14",
                            "21",
                            "22",
                            "23",
                            "24",
                            "25",
                            "26",
                            "31",
                            "32",
                            "33",
                            "34",
                            "35",
                            "41",
                            "42",
                            "43",
                            "44",
                            "51",
                            "52",
                            "53",
                            "54",
                            "61",
                            "62",
                            "63",
                            "71",
                            "72",
                            "73",
                            "74",
                            "75",
                            "81",
                            "82",
                            "83",
                            "91",
                            "92",
                            "93",
                            "94",
                            "95",
                            "96"),
                 labels = c("Commissioned armed forces officers"
                            , "Non-commissioned armed forces officers"
                            , "Armed forces occupations, other ranks"
                            , "Chief executives, senior officials and legislators"
                            , "Administrative and commercial managers"
                            , "Production and specialised services managers"
                            , "Hospitality, retail and other services managers"
                            , "Science and engineering professionals"
                            , "Health professionals"
                            , "Teaching professionals"
                            , "Business and administration professionals"
                            , "Information and communications technology professionals"
                            , "Legal, social and cultural professionals"
                            , "Science and engineering associate professionals"
                            , "Health associate professionals"
                            , "Business and administration associate professionals"
                            , "Legal, social, cultural and related associate professionals"
                            , "Information and communications technicians"
                            , "General and keyboard clerks"
                            , "Customer services clerks"
                            , "Numerical and material recording clerks"
                            , "Other clerical support workers"
                            , "Personal service workers"
                            , "Sales workers"
                            , "Personal care workers"
                            , "Protective services workers"
                            , "Market-oriented skilled agricultural workers"
                            , "Market-oriented skilled forestry, fishery and hunting workers"
                            , "Subsistence farmers, fishers, hunters and gatherers"
                            , "Building and related trades workers, excluding electricians"
                            , "Metal, machinery and related trades workers"
                            , "Handicraft and printing workers"
                            , "Electrical and electronic trades workers"
                            , "Food processing, wood working, garment and other craft and related trades workers"
                            , "Stationary plant and machine operators"
                            , "Assemblers"
                            , "Drivers and mobile plant operators"
                            , "Cleaners and helpers"
                            , "Agricultural, forestry and fishery labourers"
                            , "Labourers in mining, construction, manufacturing and transport"
                            , "Food preparation assistants"
                            , "Street and related sales and service workers"
                            , "Refuse workers and other elementary workers"))

occgroupsn <- as.numeric(occgroups)
hist(occgroupsn, labels = TRUE)

如何让它们出现?或者我应该以不同的方式做到这一点?

编辑:这是与可重复的样本相同的代码:

SampleVar <- c(1, 1, 2, 2, 2, 2, 3, 3, 3, 4) 
SampleVarLabeled <- ordered(SampleVar,  
                    levels = c("1", "2", "3", "4"), 
                    labels = c("Commissioned armed forces officers" , 
                               "Noncommissioned armed forces officers" ,
                               "Armed forces, other ranks" , 
                               "Chief executives")) 
SampleVarLabeledn <- as.numeric(SampleVarLabeled) 
hist(SampleVarLabeledn, labels = TRUE)

1 个答案:

答案 0 :(得分:0)

好的,这就是我要做的。由于您使用的是hist的频率版本,因此您还可以使用带有计数的barplot。在你的情况下,由于职业名称很长,你应该考虑一个水平的条形图。它更容易阅读。在这里,我使用table计算频率:

original_par <-par() #make a copy of your par
par(mar = c(5, 17, 2, 2))  #increase left  margin to 17
counts <- table(SampleVarLabeled)
barplot(counts, main="Occupation", xlab="Count",horiz=TRUE, las=1)

par() <- original_par #to go back to original par

enter image description here