GGplot 对象不会根据数据框中指定的颜色对条形图进行着色 [英] GGplot object does not color bars according to color specified in the dataframe

查看:52
本文介绍了GGplot 对象不会根据数据框中指定的颜色对条形图进行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的数据框:

Cum<-structure(list(Age.group = c("00-04", "00-04", "05-14", "05-14", 
                                  "15-24", "15-24", "25-49", "25-49", "50-64", "50-64", "65-79", 
                                  "65-79", "80+", "80+"), Gender = c("Female", "Male", "Female", 
                                                                     "Male", "Female", "Male", "Female", "Male", "Female", "Male", 
                                                                     "Female", "Male", "Female", "Male"), Cases = c(64578, 70518, 
                                                                                                                    187568, 197015, 414405, 388138, 1342394, 1206168, 792180, 742744, 
                                                                                                                    400232, 414613, 282268, 198026), lab = c("64,578", "70,518", 
                                                                                                                                                             "187,568", "197,015", "414,405", "388,138", "1,342,394", "1,206,168", 
                                                                                                                                                             "792,180", "742,744", "400,232", "414,613", "282,268", "198,026"
                                                                                                                    ), Age.group.Sum = c(135096, 135096, 384583, 384583, 802543, 
                                                                                                                                         802543, 2548562, 2548562, 1534924, 1534924, 814845, 814845, 480294, 
                                                                                                                                         480294), lab2 = c("135,096", "135,096", "384,583", "384,583", 
                                                                                                                                                           "802,543", "802,543", "2,548,562", "2,548,562", "1,534,924", 
                                                                                                                                                           "1,534,924", "814,845", "814,845", "480,294", "480,294"), color = c("#4285f4", 
                                                                                                                                                                                                                               "#4285f4", "#90a9e0", "#90a9e0", "#dd9e5f", "#dd9e5f", "#b45f06", 
                                                                                                                                                                                                                               "#b45f06", "#b45f06", "#b45f06", "#dd9e5f", "#dd9e5f", "#aebbd6", 
                                                                                                                                                                                                                               "#90a9e0"), Range = c("LESS THAN 74.5K", "LESS THAN 74.5K", "148.9K - 223.4K", 
                                                                                                                                                                                                                                                     "148.9K - 223.4K", "372.3K - 446.7K", "372.3K - 446.7K", "MORE THAN 670.1K", 
                                                                                                                                                                                                                                                     "MORE THAN 670.1K", "MORE THAN 670.1K", "MORE THAN 670.1K", "372.3K - 446.7K", 
                                                                                                                                                                                                                                                     "372.3K - 446.7K", "223.4K - 297.8K", "148.9K - 223.4K")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                    -14L))

正如您所看到的,虽然数据集在 color 列中具有正确的 color 值,但绘图不正确,因为例如 LESS THAN 74.5K 应该用 #4285f4 着色,而是用 #aebbd6

As you can see while the dataset has the correct color values in the color column the plot is not correct since for example the LESS THAN 74.5K should be colored #4285f4 but instead is colored with #aebbd6

names(mycols) <- mycols
ylab <- c(0.5,1,1.5,2,2.5)
ggplot_obj <- ggplot(data = Cum, aes(x = `Age.group`, y = Cases, group = Gender,fill = Range)) +
  geom_bar(aes(
    # Define a text object here that can be use for reference by ggplot_ly
    # thought ggplot will throw a warning
    text = paste("<b>Gender:</b>", Gender, "<br><b>Age:</b>", `Age.group` ,
                 "<br><b>Cases:</b>", lab, "<br><b>Total cases in age group:</b>",
                 lab2)), 
    position = "dodge", stat = "identity") +
  geom_text(aes(y = Cases + 10000, label = Gender), vjust = 1,
            position = position_dodge(width=0.9),size=2) +
  scale_fill_manual(values = Cum$color) +
  coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
  theme_bw()+ theme(
    # remove the vertical grid lines
    panel.grid.major.x = element_blank(),
    panel.border = element_blank(), axis.line.x = element_line()
  ) +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))+
  xlab("Age group") 

#> Warning: Ignoring unknown aesthetics: text
# running ggplotly with tooltip option reference to the text defined in ggplot object
ggplotly(ggplot_obj, tooltip="text") %>%
  config(modeBarButtonsToRemove = c('toImage', "zoom2d", "toggleSpikelines",
                                    "hoverClosestCartesian", "hoverCompareCartesian", "drawline", "autoScale2d",
                                    "resetScale2d", "zoomIn2d", "zoomOut2d", "pan2d", 'select2d', 'lasso2d')) %>%
  config(displaylogo = FALSE)

推荐答案

指定命名向量

lbls <- setNames(unique(Cum$color), unique(Cum$Range))
                                                                                                    
                                                                                                                                                                                                                                                                                                                                                
ggplot_obj <- ggplot(data = Cum, aes(x = `Age.group`, y = Cases, group = Gender,fill = Range)) +
  geom_bar(aes(
   
    text = paste("<b>Gender:</b>", Gender, "<br><b>Age:</b>", `Age.group` ,
                 "<br><b>Cases:</b>", lab, "<br><b>Total cases in age group:</b>",
                 lab2)), 
    position = "dodge", stat = "identity") +
  geom_text(aes(y = Cases + 10000, label = Gender), vjust = 1,
            position = position_dodge(width=0.9),size=2) +
  scale_fill_manual(values = lbls) +
  coord_cartesian(ylim = c(0, max(Cum$Cases)*1.1), expand = FALSE) +
  theme_bw()+ theme(
    # remove the vertical grid lines
    panel.grid.major.x = element_blank(),
    panel.border = element_blank(), axis.line.x = element_line()
  ) +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))+
  xlab("Age group") 

-测试

为了测试使用了不同的颜色以使其更明显

For testing used a different color to make this more visible

lbls
 #LESS THAN 74.5K  148.9K - 223.4K  372.3K - 446.7K MORE THAN 670.1K  223.4K - 297.8K 
 #       "yellow"            "red"           "blue"          "green"         "orange" 

检查ggplot_obj

这篇关于GGplot 对象不会根据数据框中指定的颜色对条形图进行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆