雷达图中geom_polygon的不同颜色

时间:2016-08-11 16:08:38

标签: r ggplot2 polygon fill radar-chart

如何用不同的颜色填充geom_polygon,而不仅仅是#3232ff?任何帮助深表感谢!我试图将“填充”设置为cbPalette变量,但不幸的是,这似乎不起作用?

这是我的数据:

C1,Criteria 1,1
C2,Criteria 1,6
C3,Criteria 1,4
C4,Criteria 1,4
C1,Criteria 2,4
C2,Criteria 2,2
C3,Criteria 2,6
C4,Criteria 2,2
C2,Criteria 3,6
C3,Criteria 3,6
C3,Criteria 3,6
C4,Criteria 3,6
C1,Criteria 4,4
C2,Criteria 4,6
C3,Criteria 4,2
C4,Criteria 4,6
C1,Criteria 5,6
C2,Criteria 5,1
C3,Criteria 5,6
C4,Criteria 5,4
C1,Criteria 6,4
C2,Criteria 6,6
C3,Criteria 6,4
C4,Criteria 6,6
C1,Criteria 7,6
C2,Criteria 7,4
C3,Criteria 7,4
C4,Criteria 7,6
C1,Criteria 8,6
C2,Criteria 8,2
C3,Criteria 8,2
C4,Criteria 8,4

这是我的代码:

dataset = read.csv("data.csv", header=FALSE, dec=",") 
colnames(dataset) = c("type", "variable", "value")
dataset$value = as.numeric(dataset$value)

dataset$variable <- factor(dataset$variable, levels = rev(dataset$variable), ordered=TRUE)

 # Radar function ------------------------------------------------------------
 coord_radar <- function (theta = "x", start = 0, direction = 1) {theta <- match.arg(theta, c("x", "y"))
   r <- if (theta == "x")
"y"
  else "x"
   ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, direction = sign(direction), is_linear = function(coord) TRUE)}


     # Radar plot ------------------------------------------------------------
     radar <- ggplot(dataset, aes(x = variable, y = value)) +
    geom_polygon(aes(group = type, color = type), fill="#3232ff", size = 1, alpha=0.2) +
    geom_line(aes(group = type, color = type), size = 1) +
  #  ggtitle("Plot title") +
  xlab("") + 
  ylab("") +
  ylim(1,10) +
  scale_y_continuous(limits=c(0,6), breaks=c(1,2,3,4,5,6,7,8,9,10), expand=c(0,2)) + 
  guides(color = guide_legend(ncol=2)) +
  coord_radar() +
  guides(colour=guide_legend(nrow=4, byrow=TRUE), shape=guide_legend(nrow=1, byrow=TRUE)) + 
  scale_colour_manual(values=cbPalette) + 
  theme(
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    legend.key = element_blank(),
    legend.title = element_blank(),
    legend.background = element_rect(color="#ffffff", fill="transparent"), ### neu check !!!
    panel.background = element_rect(fill = "white", colour = "white", size = 0.1, linetype = "solid"),
    panel.grid.major = element_line(size = 0.1, linetype = 'solid', colour = "#dddddd")
  )

enter image description here

0 个答案:

没有答案