[英]Using r and ggplot How do I specify specific hexadecimal color/colour values to use to represent different character values
Assuming is.character(MyColours) = TRUE
where MyColours contains a number of hexadecimal colour values. 假设
is.character(MyColours) = TRUE
,其中MyColours包含许多十六进制颜色值。
How do I ensure that when I want to plot using ggplot I can ensure that MyColours[1] will always be used to represent "Ford" and MyColour[3] will represent "Toyota" when I can't guarantee the order "Ford" or "Toyota" will appear in the observations I use to plot. 如何确保当我要使用ggplot进行绘图时,可以确保当我不能保证订单“ Ford”时,MyColours [1]将始终用于表示“福特”,而MyColour [3]将始终用于表示“丰田”。或“丰田”将出现在我用于绘制的观测图中。
While the geo_bar example below was helpful and did remind me to add column names to MyColours
I'm hoping to plot lines not bars 虽然下面的geo_bar示例很有帮助,并且确实提醒我向
MyColours
添加列名称, MyColours
我希望绘制线条而不是线条
ggplot(MyData, aes(x = TimePeriod, y = CountOfItems,
group = Account, color = scale_fill_manual(values = MyColours)))
+ geom_line(size = 1)
+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
I get the error Aesthetics must either be length one, or the same length as the dataProblems:scale_fill_manual(values = MyColours)
even when length(MyColours)
is equal to length(unique(MyData$Account))
我收到错误
Aesthetics must either be length one, or the same length as the dataProblems:scale_fill_manual(values = MyColours)
即使length(MyColours)
等于length(unique(MyData$Account))
Sample Data 样本数据
dput(MyData)
structure(list(Account = structure(c(1L, 2L, 1L, 2L), .Label = c("Mazda RX4",
"Toyota Corolla"), class = "factor"), CountOfItems = c(14, 120,
23, 345), TimePeriod = structure(c(1L, 2L, 2L, 3L), .Label = c("2010-12",
"2011-01", "2011-02"), class = "factor")), .Names = c("Account",
"CountOfItems", "TimePeriod"), row.names = c(NA, -4L), class = "data.frame")
Is it possible to use scale_fill_manual with line plots? 可以将scale_fill_manual与折线图一起使用吗?
You could name MyColours
, eg like this: 您可以这样命名
MyColours
:
MyColour <- c("#FF0000", "#00FF00", "#0000FF")
names(MyColour) <- c("Mazda RX4", "Toyota Corolla", "Fiat 128")
df <- mtcars[c("Mazda RX4", "Toyota Corolla", "Fiat 128"), ]
df$car <- rownames(df)
library(ggplot2)
ggplot(df, aes(x = car, y = mpg, fill = car)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = MyColour)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.