R 并排箱线图,在ggplot中有正确的图例标签
我正在创建一个由十六进制颜色着色的并排箱线图。我似乎有它的工作,但无法让图例(在绘图右侧)有切割标签(例如:“公平”而不是“#F8766D”)。以下是一个模拟我的问题的MWE:R 并排箱线图,在ggplot中有正确的图例标签,r,ggplot2,R,Ggplot2,我正在创建一个由十六进制颜色着色的并排箱线图。我似乎有它的工作,但无法让图例(在绘图右侧)有切割标签(例如:“公平”而不是“#F8766D”)。以下是一个模拟我的问题的MWE: library(ggplot2) library(dplyr) df <- diamonds[sample(1:nrow(diamonds), size = 1000),] colList = scales::hue_pal()(5) df <- as.data.frame(df) df <- df
library(ggplot2)
library(dplyr)
df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
colList = scales::hue_pal()(5)
df <- as.data.frame(df)
df <- df %>% select("cut","price")
saveColor <- df %>% transmute(color = plyr::mapvalues(cut, c("Fair", "Good", "Very Good", "Premium", "Ideal"), colList))
df$color <- unlist(saveColor)
ggplot(df, aes(cut, price)) + geom_boxplot(aes(fill = factor(color))) + scale_colour_manual(labels = c("Fair", "Good", "Very Good", "Premium", "Ideal"), values = colList)
库(ggplot2)
图书馆(dplyr)
df不确定您正试图做什么,但通常不会将颜色作为列添加到数据集中。相反,您只需将它们作为值提供给颜色比例。还要注意,这使您的代码更加简单
df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
colList = scales::hue_pal()(5)
df <- df %>% select("cut","price")
ggplot(df, aes(cut, price)) +
geom_boxplot(aes(fill = cut)) +
scale_fill_manual(values = colList)
df不确定您要做的到底是什么,但通常不会将颜色作为列添加到数据集中。相反,您只需将它们作为值提供给颜色比例。还要注意,这使您的代码更加简单
df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
colList = scales::hue_pal()(5)
df <- df %>% select("cut","price")
ggplot(df, aes(cut, price)) +
geom_boxplot(aes(fill = cut)) +
scale_fill_manual(values = colList)
df我想知道您是否只想将scale\u color\u manual()
更改为scale\u fill\u manual()
@jazzurro是的,这解决了问题,尽管代码仍然过于复杂。没有必要在数据框中添加颜色。@ClausWilke是的,我明白你的意思。我只是想指出一个快速解决问题的方法。我想知道您是否只想将scale\u color\u manual()
更改为scale\u fill\u manual()
@jazzurro是的,这解决了问题,尽管代码仍然过于复杂。没有必要在数据框中添加颜色。@ClausWilke是的,我明白你的意思。我只是想指出一个快速解决问题的方法。