我被要求重新创建以下情节样式。 (请忽略以下问题:这是否是一种很好的可视化类型,请明智地将其视为在数字表中添加彩色元素。)

大部分都非常简单,但是我还没有找到使中心变空心的好方法。为了节省时间,我可能会增加添加看不见的虚拟数据的麻烦(如果没有其他人这样做,我会发布这种方法,但似乎不如修改主题的最优方法)。是否有基于主题的解决方案或非ggplot2 R解决方案?

我们在模仿什么



简单的ggplot2结果(不需要的填充中心)

library(ggplot2)

# make sample dataframe

Category <- c("Electronics", "Appliances", "Books", "Music", "Clothing",
            "Cars", "Food/Beverages", "Personal Hygiene",
            "Personal Health/OTC", "Hair Care")
Percent <- c(81, 77, 70, 69, 69, 68, 62, 62, 61, 60)

internetImportance<-data.frame(Category,Percent)

# append number to category name
internetImportance$Category <-
     paste0(internetImportance$Category," - ",internetImportance$Percent,"%")

# set factor so it will plot in descending order
internetImportance$Category <-
    factor(internetImportance$Category,
    levels=rev(internetImportance$Category))

# plot

ggplot(internetImportance, aes(x = Category, y = Percent,
    fill = Category)) +
    geom_bar(width = 0.9, stat="identity") +
    coord_polar(theta = "y") +
    xlab("") + ylab("") +
    ylim(c(0,100)) +
    ggtitle("Top Product Categories Influenced by Internet") +
    geom_text(data = internetImportance, hjust = 1, size = 3,
              aes(x = Category, y = 0, label = Category)) +
    theme_minimal() +
    theme(legend.position = "none",
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line = element_blank(),
          axis.text.y = element_blank(),
          axis.text.x = element_blank(),
          axis.ticks = element_blank())

我们如何绘制带有空心中心的数据?

最佳答案

这是一个使用plotrix包的非ggplot2(基本R图形)解决方案,其中包含两个不错的函数:draw.circle()draw.arc():

circBarPlot <- function(x, labels, colors=rainbow(length(x)), cex.lab=1) {
  require(plotrix)
  plot(0,xlim=c(-1.1,1.1),ylim=c(-1.1,1.1),type="n",axes=F, xlab=NA, ylab=NA)
  radii <- seq(1, 0.3, length.out=length(x))
  draw.circle(0,0,radii,border="lightgrey")
  angles <- (1/4 - x)*2*pi
  draw.arc(0, 0, radii, angles, pi/2, col=colors, lwd=130/length(x), lend=2, n=100)
  ymult <- (par("usr")[4]-par("usr")[3])/(par("usr")[2]-par("usr")[1])*par("pin")[1]/par("pin")[2]
  text(x=-0.02, y=radii*ymult, labels=paste(labels," - ", x*100, "%", sep=""), pos=2, cex=cex.lab)
}

circBarPlot(Percent/100, Category)
text(0,0,"GLOBAL",cex=1.5,col="grey")

它给了我:

关于r - 制作一个具有空心中心的圆形条形图(又名赛道图),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15751442/

10-13 07:37