假设is.character(MyColours) = TRUE
其中MyColours包含许多十六进制颜色值。
我如何确保当我想使用ggplot绘图时,我可以确保MyColours [1]将始终用于表示“Ford”,MyColour [3]将代表“Toyota”,当我无法保证订单时“福特”或“丰田”将出现在我用来绘制的观察中。
虽然下面的geo_bar示例很有用,但确实提醒我将列名称添加到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))
即使Aesthetics must either be length one, or the same length as the dataProblems:scale_fill_manual(values = MyColours)
等于length(MyColours)
length(unique(MyData$Account))
示例数据
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")
是否可以将scale_fill_manual与线图一起使用?
答案 0 :(得分:5)
您可以命名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)