如何用x值代替ggplot2中的图例?

时间:2018-12-01 17:19:47

标签: r ggplot2

因此,我正在尝试构建一个类似如下的圆条图:

library(tidyverse)

a <- c("Like", "Dislike", "Neutral")
b <- c("Price", "Functionality", "Functionality: missing pieces", "Durability")
n <- c(10, 0, 5, 6,5,4,19,28,4,6,9,1)

data <- data.frame(a,b,n)

ggplot(data, aes(x=b, y= n, fill = a))+geom_bar(stat="identity")+
ylim(-100, 120)+
coord_polar(start = 0)

但是我想保持它的清洁,并将条形的名称放在图例框中而不是在条形上方。将名称替换为数字,然后将其链接到图例将是理想的。

3 个答案:

答案 0 :(得分:2)

您可以将它们绘制为形状:

data <- data.frame(a, b, n) %>% 
  mutate(b_fct = as.factor(b))

shapes <- data$b_fct %>%
  levels() %>% 
  length() %>% 
  seq() %>% 
  as.character() %>%
  map_int(utf8ToInt)

ggplot(data, aes(x = b_fct, shape = b, y = n, fill = a)) +
  geom_bar(stat = "identity") +
  geom_point(y = 120) +
  ylim(-100, 120) +
  coord_polar(start = 0) +
  scale_x_discrete(labels = NULL) +
  scale_shape_manual(values = shapes)

enter image description here

答案 1 :(得分:1)

您可以将nohup daphne -b 0.0.0.0 -p 3031 asgi:channel_layer & nohup python manage.py runworker & 转换为因子,然后稍微滥用b

scale_color_manual

enter

答案 2 :(得分:0)

使用字幕功能,也许是这样的:

a <- c("Like", "Dislike", "Neutral")
b <- as.character(seq_len(4))
n <- c(10, 0, 5, 6,5,4,19,28,4,6,9,1)

data <- data.frame(a,b,n)

ggplot(data, aes(x=b, y= n, fill = a))+geom_bar(stat="identity")+
ylim(-100, 120)+
coord_polar(start = 0) +
    labs(caption=paste((paste(seq_len(4), c("Price", "Functionality", "Functionality: missing pieces", "Durability"), sep=": ")), collapse="\n")) +
theme (plot.caption=element_text(hjust=0))