将两个饼图合二为一

时间:2014-06-01 20:13:29

标签: r plot pie-chart

我正在尝试使用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")

我得到的数字is

现在我如何配置图表以使国家/地区具有相同的配色方案?并且他们按照相同的顺序分两部分,首先它应该是美国和英国等等。

两个简化了我想在一个饼图中制作两个饼图的问题,其中一半的饼图代表2009年,另一半代表2010年。

请帮助。

谢谢

4 个答案:

答案 0 :(得分:12)

 lbls0 <- 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")


 nlbl <- length(lbls)
 yrs <- rep(2009:2010,each=nlbl/2)
 xlbls <- sprintf("%s (%d) %d%%",lbls0,yrs,pct)
 pie(slices,labels = xlbls, col=rep(rainbow(nlbl/2),2),
      main="Pie Chart of Countries")

您是否愿意考虑一种可视化的形式,以便更容易进行定量比较?

 library(ggplot2); theme_set(theme_bw())
 dd <- data.frame(lbls0,yrs,slices,pct)
 dd$lbls0 <- reorder(dd$lbls0,-dd$slices)
 ggplot(dd,aes(x=lbls0,y=slices,fill=lbls0))+
    geom_bar(aes(alpha=factor(yrs)),
                    stat="identity",position=position_dodge(width=1))+
    scale_alpha_discrete(range=c(0.7,1.0))+
    geom_text(aes(label=paste0(pct,"%"),
              group=interaction(lbls0,yrs)),hjust=-0.2,
              position=position_dodge(width=1))+
                  coord_flip()+
    expand_limits(y=20)+labs(x="",y="total")

enter image description here

如果您对国家/地区之间的比较更感兴趣,而不是国家/地区内的年份,则可以跳过scale_alpha内容并使用facet_wrap(~yrs) ...

答案 1 :(得分:8)

您可以借助reshape2包解决此问题:

# reading the data
df <- read.table(header=TRUE, text="Country   2009    2010
US  10  12
UK  13  14
Germany 18  11
China   9   8
Malaysia    7   15
Others  13  15")

# setting the column names correct
colnames(df) <- c("Country","2009","2010")

# reshaping the dataframe
require(reshape2)
df2 <- melt(df, id="Country")

# creating the plot
pie(df2$value, labels=paste0(df2$Country," (",df2$variable,") ",df2$value,"%"),
    col=rainbow(length(levels(df2$Country))), main="Pie Chart of Countries")

给出: enter image description here


有关年份标签的变体,您还可以使用:

pie(df2$value, labels=paste0(df2$Country," ",df2$value,"%"),
    col=rainbow(length(levels(df2$Country))), main="Pie Chart of Countries")
mtext("2009",side=3)
mtext("2010",side=1)

给出: enter image description here


正如@BenBolker所说,饼图不是一种很好的呈现数据的方式。值得引用的是帮助页面?pie,这也得到了认可:

  

饼图是显示信息的一种非常糟糕的方式。眼睛擅长判断线性测量,判断相对区域不好。条形图或圆点图是显示此类数据的首选方式。

     

Cleveland(1985),第264页:“可以通过饼图显示的数据始终可以通过点图显示。这意味着可以做出沿着共同尺度的位置判断,而不是不太准确的角度判断。“这个陈述是基于克利夫兰和麦吉尔的实证调查以及感性心理学家的调查。

答案 2 :(得分:4)

这可能有用。至少两个半部具有相同的配色方案。我不确定你的意思是同一个订单。

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代码
col  <- rep(c("yellow", "orange", "red", "purple", "blue", "green"),2)

我不确定你对这两半你想要什么。如果你想将每一半的百分比标准化为50%,这可能有效:

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

答案 3 :(得分:3)

这是另一个类似于我的回答here

的解决方案
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)


dat <- data.frame(year = rep(c(2009, 2010), each = 6), lbls, slices)
dat$total <- with(dat, ave(slices, year, FUN = sum))


plot.new()

par(new = TRUE)
pie(dat$slices, border = NA, radius = 1,
    col = rainbow(length(lbls) / 2), labels = lbls,
    main = 'Pie Chart of Countries')

par(new = TRUE)
c2 <- c('grey50', 'grey75')[factor(dat$year)]
pie(dat$slices, border = c2, radius = .7, col = c2, labels = NA)

text(0, c(.3, -.3), 2009:2010)

enter image description here