如何使用ggplot2在R中使用轴标签和旋转格式设置雷达图

时间:2019-04-13 21:14:13

标签: r ggplot2 radar-chart

我在r中有一个雷达图,按月显示百分比。我想

  • 更改图表,以使Jan从90%的角度开始而不是旋转 在右边
  • 更改图表,以便百分比标签显示在图表中,而不是在左侧

错误的图表在下面

Rad Bad

下面我要复制的好图表

enter image description here

下面是雷达图的代码

library(reshape2)
library(ggplot2)
library(dplyr)

Group <- factor(c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
Urban <- c(-0.61, 0.13, 0.24, -0.30, -0.12, -1.24, 0.74, 0.55, 0.80, .2, .2, .2)
Rural <- c(1.02, -0.40, 0.73, 0.17, 0.68, 1.21, -1.35, -0.84, -1.27, .2, .2, .2)
Total <- c(0.41, -0.27, 0.97, -0.13, 0.56, -0.03, -0.61, -0.29, -0.47, 0.4, 0.4, 0.4)

# data preparation
df = data.frame(Group = Group,
                Urban = Urban,
                Rural = Rural,
                Total = Total)    
df.m <- melt(df, 
             id.vars = c("Group"), 
             measure.vars = c("Urban", "Rural","Total"),
             variable.name = "Demographics",
             value.name = "Percentage")

# plot
ggplot(data = df.m,
       aes(x = Group, y = Percentage, group = Demographics, colour = Demographics)) + 
  geom_polygon(size = 1, alpha= 0.2) + 
  ylim(-2.0, 2.0) + ggtitle("Radar")  + 
  scale_x_discrete() +
  theme_light() +
  scale_color_manual(values = c("Red", "Blue","Black")) +
  scale_fill_manual(values = c("Red", "Blue","Black")) +
  coord_polar()

1 个答案:

答案 0 :(得分:1)

我不知道使用正常的scale_y_continuous项来使y轴比例出现在绘图中而不是在其边缘出现的方法,但是可以使用annotate伪造它来创建一个临时图层。我们还可以使用start中的coord_polar项来旋转极坐标变换。

ggplot(data=df.m,  aes(x=Group, y=Percentage, group= Demographics, colour=Demographics )) + 
  annotate("text", x = 1, y = -2:2, label = -2:2, hjust = 1) +
  geom_polygon(size = 1, alpha= 0.2) + 
  ggtitle("Radar")  + 
  scale_y_continuous(labels = NULL) +
  scale_x_discrete() +
  scale_color_manual(values= c("Red", "Blue","Black"))+
  scale_fill_manual(values= c("Red", "Blue","Black"))+
  theme_light()+
  coord_polar(start = -pi/12)

enter image description here