[英]How do you map dynamic colors in ggplot using a highlighter column?
[英]how do you set custom colors in ggplot heatmaps
structure(list(Date = structure(c(1372698000, 1291129200, 1291388400,
1298646000, 1386007200, 1295017200, 1382104800, 1385240400, 1265986800,
1364835600), class = c("POSIXct", "POSIXt"), tzone = ""), Logons = c(" 973,675",
" 710,782", " 734,635", " 812,793", "1,126,432", " 916,832",
"1,011,911", "1,974,513", " 674,884", "1,278,295"), Month = structure(c(7L,
11L, 12L, 2L, 12L, 1L, 10L, 11L, 2L, 4L), .Label = c("January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"), class = "factor"),
Max = c(973675L, 710782L, 734635L, 812793L, 1126432L, 916832L,
1011911L, 1974513L, 674884L, 1278295L), Year = c("2013",
"2010", "2010", "2011", "2013", "2011", "2013", "2013", "2010",
"2013")), .Names = c("Date", "Logons", "Month", "Max", "Year"
), row.names = c(453L, 2564L, 2636L, 3343L, 3435L, 4545L, 5275L,
5786L, 7382L, 8077L), class = "data.frame")
我正在嘗試創建一個熱圖,其中Year將在y軸上,而Month將在x軸上。
我這樣做:
ggplot(y ,aes(Month, Year, fill=Logons, label=paste(paste(weekdays(Date), format(Date,"%H:%M"), sep="\n"), "\n",Logons))) + geom_tile() + theme_bw() + guides(fill = guide_legend(keywidth = 5, keyheight = 1)) + theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) + geom_text(size=3)+ scale_colour_manual(breaks = c(200000, 400000, 800000, 1000000, 1300000, 1500000), labels = c("0 month", "1 month", "3 months","6 months", "9 months", "12 months"),values = c("#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2", "#D55E00"))
不工作。 我的傳奇中有很多項目,我的熱圖中只需要6個項目和6個顏色代碼。 任何想法為什么這不起作用?
數據中的第一個問題是Logons
被視為因子而不是數字,因為數字中有逗號。 所以你必須將它們轉換為數字。
y$Logons<-as.numeric(gsub(",","",y$Logons))
正如您使用fill=
for geom_tile()
那么您必須使用scale_fill_...
來更改填充值。 在這種情況下,使用scale_fill_gradientn()
來設置填充值的格式。
ggplot(y ,aes(Month, Year, fill=Logons,
label=paste(paste(weekdays(Date), format(Date,"%H:%M"), sep="\n"), "\n",Logons))) +
geom_tile() + theme_bw() +
guides(fill = guide_legend(keywidth = 5, keyheight = 1)) +
theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) +
geom_text(size=3)+
scale_fill_gradientn(limits=c(200000, 1500000),
breaks = c(200000, 400000, 800000, 1000000, 1300000, 1500000),
labels = c("0 month", "1 month", "3 months","6 months", "9 months", "12 months"),
colours = c("#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2", "#D55E00"))
此外,我認為您應該從代碼中刪除guides(fill = guide_legend(keywidth = 5, keyheight = 1))
行,因為填充顏色會更改為漸變,但現在您在圖例中得到的離散值與繪圖中的顏色不完全匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.