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

Showing (value) labels in a histogram in R

我在 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)

好的,这就是我的做法。由于您使用的是 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