Include values to the barplot and pie charts in R

爱⌒轻易说出口 提交于 2019-12-12 05:56:01

问题


I have data in this form:

proprete.freq <- table(cnData$proprete)
proprete.freq.genre <-table(cnData$genre,cnData$proprete)

I am using these functions (barplot and pie) to plot the data:

barplot(proprete.freq.genre, col = heat.colors(length(rownames(proprete.freq.genre)))
    , main="Proprete", beside = TRUE)

pie(proprete.freq, col=rainbow(3), names.arg=avis, main="Propreté")

Here is the result:

Question: How to include the value just on top of the barplots and below the categorical variables for the pie. Something like that:


回答1:


Here is a general approach:

  1. pie has a labels parameter so you can just use that, use \n for a line break, and add text under the name
  2. barplot has a return value which are the x-coordinates of each bar, so just use text along with the data (for the y-coordinates), and use pos = 3 to put the label above that {x, y} point.

ex:

par(mfrow = c(2, 1))
pie(1:3, labels = sprintf('%s\n%s%%', 1:3, round(1:3 / 6 * 100)))
bp <- barplot(VADeaths, beside = TRUE)
text(c(bp), c(VADeaths), labels = c(VADeaths), pos = 3, xpd = NA)



来源:https://stackoverflow.com/questions/28909843/include-values-to-the-barplot-and-pie-charts-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!