首页 > 解决方案 > 将每列设置为其自己的调色板

问题描述

我正在制作我在各种项目上花费的时间量的柱形图,每个项目用于一系列“客户”(实际上是我工作的不同“领域”)中的一个,使用优秀的togglr软件包下载我的跟踪时间数据和ggplot2。

我正在使用的代码是这样的:(数据 dput(SO) 输出粘贴在问题下方)

library("ggplot2")
library("RColorBrewer")
theme_set(theme_bw())
colourCount = 48 #nrow(projects)
getPalette = colorRampPalette(brewer.pal(12, "Paired"))

ggplot(data = SO, aes(x = client, y = time_spent)) +
  geom_col(aes(fill = area_project), colour = "black") +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme(legend.position = "right") +
  guides(fill=guide_legend(ncol = 2)) +
  ggtitle("From Start to End") #paste("From", date(min(df$start)), " to", date(max(df$stop)))) +
  xlab("Functional Area") + ylab("Hours")

这产生了这个情节:

时间跟踪图

我不知道该怎么做是让每一列都有自己的调色板,每个项目都有不同的色调。

IE 我希望“0_Admin”列中的所有框都是不同的蓝色,“1_Monitoring”列中的每个框都是不同的绿色,等等。上面的情节很接近,但主要是巧合和项目数量/区域。例如,您会注意到“3_Management”项目既是红色又是橙色,橙色阴影一直“流血”到“7_Visitor Safety”。

随着时间的推移,项目的数量总体上会增加(但在我报告较小的时间段时将是一个子集),因此完全手动的规模是不可行的,但区域的数量将保持不变。

有什么想法吗?提示?谢谢!

SO <- structure(list(client = c("0_Admin", "0_Admin", "0_Admin", "0_Admin",
                                "0_Admin", "0_Admin", "0_Admin", "0_Admin", "0_Admin", "1_Monitoring",
                                "1_Monitoring", "1_Monitoring", "1_Monitoring", "1_Monitoring",
                                "1_Monitoring", "1_Monitoring", "2_Science", "2_Science", "2_Science",
                                "2_Science", "2_Science", "2_Science", "3_Management", "3_Management",
                                "3_Management", "3_Management", "3_Management", "3_Management",
                                "4_EA", "6_Fire", "6_Fire", "7_VisitorSafety", "8_ResConMisc",
                                "8_ResConMisc", "8_ResConMisc", "8_ResConMisc", "8_ResConMisc",
                                "8_ResConMisc", "8_ResConMisc", "8_ResConMisc", "8_ResConMisc",
                                "9_CrossFxn", "9_CrossFxn", "9_CrossFxn", "9_CrossFxn", "Z_Leave",
                                "Z_Leave", "Z_Leave"), 
                     project = c("Email", "EPM", "Finance",
                                 "HR", "Misc", "OHS", "RCPs", "Time mgmt", "Training", "Amphibians",
                                 "Area burned", "Birds", "Rangeland Health", "Sediment", "Ungulates",
                                 "Water Quality", "Bison GPS", "Bison science advisory group",
                                 "Collaboration", "Corridor Use", "Grassland bird survey", "Misc",
                                 "Beavers", "Bison", "Geese", "HUMP/HIP", "HWC", "Invasive Plants",
                                 "Nest sweeps", "Fire crew", "Fire mgmt plan", "DO response",
                                 "Duty Officer", "Media", "Misc", "Open Data", "Peer discussion",
                                 "RC meeting", "Training", "Travel", "Work planning", "CC meeting",
                                 "Events", "Misc", "Trails", "Appointments", "Stat holiday", "Vacation"
                                ), 
                     time_spent = c(174.709722222222, 15.2483333333333, 26.7827777777778,
                                    127.603611111111, 21.7127777777778, 6.32222222222222, 11.9725,
                                    3.32111111111111, 29.6375, 4.80333333333333, 0.498055555555556,
                                    74.4958333333333, 21.8011111111111, 1.14111111111111, 21.5008333333333,
                                    36.0780555555556, 1.44972222222222, 1.40694444444444, 6.83916666666667,
                                    3.93027777777778, 6.94916666666667, 2, 28.7986111111111, 154.448888888889,
                                    0.684444444444445, 12.5727777777778, 2.98861111111111, 1.89416666666667,
                                    1.75, 21.2725, 11.0122222222222, 2.74333333333333, 0.817777777777778,
                                    10.415, 84.9144444444444, 11.4, 19.7738888888889, 8.84444444444444,
                                    38.7216666666667, 8, 11.6063888888889, 10.5191666666667, 3.41638888888889,
                                    20.8216666666667, 0.298611111111111, 6.74611111111111, 30, 75.5
                                ), 
                     area_project = c("0_Email", "0_EPM", "0_Finance", "0_HR",
                                      "0_Misc", "0_OHS", "0_RCPs", "0_Time mgmt", "0_Training", "1_Amphibians",
                                      "1_Area burned", "1_Birds", "1_Rangeland Health", "1_Sediment",
                                      "1_Ungulates", "1_Water Quality", "2_Bison GPS", "2_Bison science advisory group",
                                      "2_Collaboration", "2_Corridor Use", "2_Grassland bird survey",
                                      "2_Misc", "3_Beavers", "3_Bison", "3_Geese", "3_HUMP/HIP", "3_HWC",
                                      "3_Invasive Plants", "4_Nest sweeps", "6_Fire crew", "6_Fire mgmt plan",
                                      "7_DO response", "8_Duty Officer", "8_Media", "8_Misc", "8_Open Data",
                                      "8_Peer discussion", "8_RC meeting", "8_Training", "8_Travel",
                                      "8_Work planning", "9_CC meeting", "9_Events", "9_Misc", "9_Trails",
                                      "Z_Appointments", "Z_Stat holiday", "Z_Vacation")), class = c("grouped_df",
                    "tbl_df", "tbl", "data.frame"), row.names = c(NA, -48L), vars = "client", labels = structure(list(
                      client = c("0_Admin", "1_Monitoring", "2_Science", "3_Management",
                                 "4_EA", "6_Fire", "7_VisitorSafety", "8_ResConMisc", "9_CrossFxn",
                                 "Z_Leave")), class = "data.frame", row.names = c(NA, -10L), vars = "client", drop = TRUE), 
                                indices = list(0:8, 9:15, 16:21, 22:27, 28L, 29:30, 31L, 32:40, 41:44, 45:47), 
                                drop = TRUE, group_sizes = c(9L, 7L, 6L, 6L, 1L, 2L, 1L, 9L, 4L, 3L), biggest_group_size = 9L)

标签: rggplot2colorbrewergeom-col

解决方案


推荐阅读