[英]r ggplot2 modify x axis labels of stacked bar plot with interaction aes
[英]Modify legend and labels of stacked-area plot in R/ggplot2
編輯:在評論中由Haboryme解決; 問題是我使用xlab和ylab代替x和y作為labs()的關鍵字參數的名稱(解釋圖形標簽),以及在對aes()的第二次調用中冗余使用colour =(解釋持久性)原始圖例)。
我想用R和ggplot2從一些CSV數據制作一個堆積面積圖。 例如:
In file "test.csv":
Year,Column with long name 1,Column with long name 2
2000,1,1
2001,1,1.5
2002,1.5,2
我運行以下代碼(模仿此GIS.SE問題的答案):
library(ggplot2)
library(reshape)
df <- read.csv('test.csv')
df <- melt(df, id="Year")
png(filename="test.png")
gg <- ggplot(df,aes(x=as.numeric(Year),y=value)) +
# Add a new legend
scale_fill_discrete(name="Series", labels=c("Foo bar", "Baz quux")) +
geom_area(aes(colour=variable,fill=variable)) +
# Change the axis labels and add a title
labs(title="Test",xlab="Year",ylab="Values")
print(gg)
dev.off()
結果在文件“ test.png”中:
問題:我更改軸標簽的嘗試被忽略,我的新圖例(從R Cookbook的建議中借來了代碼)被添加到(替代了)(奇怪地重新着色)默認值中。 (R Cookbook提供的其他解決方案,例如調用guides(fill = FALSE),或多或少都做同樣的事情。)我寧願不使用編輯數據幀的變通方法(例如,刪除read.csv( )替換列標題中的空格),以使默認標簽正確無誤。 我該怎么辦?
ggplot(df,aes(x=as.numeric(Year),y=value)) +
scale_fill_discrete(name="Series", labels=c("Foo bar", "Baz quux")) +
geom_area(aes(fill=variable)) +
labs(title="Test",x="Year",y="Values")
geom_area()
的aes()
中的參數colour
僅為輪廓geom_area()
,因此在此處不向繪圖添加任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.