问题描述
我正在尝试使用R中的以下数据创建一个饼图:
I am trying to create a pie chart with my following data in R:
2009 2010
US 10 12
UK 13 14
Germany 18 11
China 9 8
Malaysia 7 15
Others 13 15
我正在使用的命令是:
slices<-c(10,13,18,9,7,13,12,14,11,8,15,15)
lbls <- c("US","UK","Germany","China", "Malaysia", "Others","US","UK","Germany","China", "Malaysia", "Others")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls,"%",sep="")
lbls <- paste(lbls, pct)
pie(slices,labels = lbls, col=rainbow(length(lbls)), main="Pie Chart of Countries")
我得到的数字
现在如何配置图形,使国家/地区具有相同的配色方案?并且他们遵循相同的顺序,分为两半,首先应该是美国和英国,依此类推.
Now how can I configure the graph so that the countries have same colour scheme? and they follow the same order in two halves, like first it should be US and the UK and so on.
两个简化了这个问题,我想在一个饼图中制作两个饼图,其中一半的饼图代表2009年,另一半代表2010年.
Two simplify the question I want to make two piecharts in one piechart, where one half of piechart represents 2009 and the other half 2010.
请帮忙.
谢谢
推荐答案
这可能有效.至少两半具有相同的配色方案.我不确定同一命令的意思.
This might work. At least the two halves have the same color scheme. I am not sure what you mean by the same order.
slices<-c(10,13,18,9,7,13,12,14,11,8,15,15)
pct <- round(slices/sum(slices)*100)
lbls <- c("US","UK","Germany","China", "Malaysia", "Others","US","UK","Germany","China", "Malaysia", "Others")
lbls <- paste(lbls,"%",sep="")
lbls <- paste(lbls, pct)
col <- c("yellow", "orange", "red", "purple", "blue", "green", "yellow", "orange", "red", "purple", "blue", "green")
pie(slices,labels = lbls, main="Pie Chart of Countries", col = col)
您可以使用
col <- rep(c("yellow", "orange", "red", "purple", "blue", "green"),2)
我不确定您想要这两个半部分.如果您希望将每一半的百分比标准化为50%,则可能会起作用:
I am not certain what you want regarding the two halves. If you want to standardize the percentages to 50% for each half this might work:
a <- c(7, 9, 12, 6, 5, 9)
a2 <- (a/sum(a)) * 50
a2
# [1] 7.291667 9.375000 12.500000 6.250000 5.208333 9.375000
b <- c(8, 10, 8, 6, 10, 10)
b2 <- (b/sum(b)) * 50
b2
# [1] 7.692308 9.615385 7.692308 5.769231 9.615385 9.615385
pct <- round(c(a2,b2),2)
pct
这篇关于将两个饼图合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!