在R中:为什么我不能给图上的点着色?

t3psigkw  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(131)

在R中,我使用了一个自生成的数据框架,其中包含了选举中的政党及其席位。
问题是议会图生成正确,但构成图的点显示为黑色,无法用政党数据对应的颜色显示,请帮帮忙。
公式和 Dataframe :

library(ggplot2)
library(ggparliament)
library(readxl)
library(dplyr)

colors = c("#783736","#7ca32f","#b59f5c","#7ed61a","#a546bd","#337d34")

parties <- c("PC","FRVS","PH","PEV","COM","CS")
seats <- c(10,2,3,2,6,7)
datos <- data.frame(parties,seats,colors)

    parties  seats  colors
1       PC    10    #38ebe8
2     FRVS     2    #2979cf
3       PH     3    #1f1f9c
4      PEV     2    #7022ab
5      COM     6    #181644
6       CS     7    #e01624

我用这个公式生成了一个议会图:

congress1 <- parliament_data(election_data = datos,
                             type = "semicircle",
                             parl_rows = 6,
                             party_seats = datos$seats)

cl <- ggplot(congreso1, aes(x = x, y = y, fill = colors)) +
  geom_parliament_seats(size=3.5) + 
  theme_ggparliament() +
  labs(fill = NULL, 
       title = "Parliament Seats") +
  scale_fill_manual(values = datos$colors, 
                    limits = datos$parties) 

cl
h5qlskok

h5qlskok1#

我们也可以这样做:将fill变更为color美学,并将相应的scale_fill_manual变更为scale_color_manual

congress1 <- parliament_data(election_data = datos,
                             type = "semicircle",
                             parl_rows = 6,
                             party_seats = datos$seats)

cl <- ggplot(congress1, aes(x = x, y = y, color = parties)) +
  geom_parliament_seats(size=3.5) + 
  scale_color_manual(values = colors, 
                     limits = parties) +
  theme_ggparliament() +
  labs(color = NULL, 
       title = "Parliament Seats")

cl

j2qf4p5b

j2qf4p5b2#

我认为geom_parliament_seats是为color(又名colour)而不是fill参数化的,您可以使用scale_color_identity来逐字使用这些值,因为它们已经在ggplot中使用的 Dataframe 中指定。

ggplot(congreso1, aes(x = x, y = y, color = colors)) +
  geom_parliament_seats(size=3.5) + 
  theme_ggparliament() +
  labs(fill = NULL, 
       title = "Parliament Seats") +
  scale_color_identity()

相关问题