[英]Using r and ggplot How do I specify specific hexadecimal color/colour values to use to represent different character values
假設is.character(MyColours) = TRUE
,其中MyColours包含許多十六進制顏色值。
如何確保當我要使用ggplot進行繪圖時,可以確保當我不能保證訂單“ Ford”時,MyColours [1]將始終用於表示“福特”,而MyColour [3]將始終用於表示“豐田”。或“豐田”將出現在我用於繪制的觀測圖中。
雖然下面的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))
我收到錯誤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與折線圖一起使用嗎?
您可以這樣命名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.