2014-04-07 86 views
-1

我有我設法在ggpot中繪製的小型data.frame。由於ggplot不支持模式,我用顏色繪製數據圖。我會欣賞比我在着色和設計甚至黑白方面做得更好的演示。另外,我無法改變圖例標題ggplot2 barplot

我的數據:

structure(list(Type = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
9L, 8L), .Label = c("Type A+Mod ", "Type B+C+D", "Type E+A", 
"Type G+W", "Type H & Mod C", "Type Operation", "Type Production", 
"Type Sales", "X, T, S"), class = "factor"), X2011 = structure(c(7L, 
4L, 6L, 5L, 9L, 8L, 3L, 1L, 2L), .Label = c("$1,517.00", "$1,579.00", 
"$1,727.00", "$105,352.00", "$126,787.00", "$141,647.00", "$187,506.00", 
"$24,968", "$30,397.00"), class = "factor"), X2012 = structure(c(7L, 
6L, 5L, 4L, 8L, 9L, 3L, 2L, 1L), .Label = c("$1,232.00", "$1,406.00", 
"$1,963.00", "$109,533.00", "$125,795.00", "$166,251.00", "$172,238.00", 
"$18,040.00", "$23,541.00"), class = "factor"), X2013 = structure(c(8L, 
4L, 3L, 2L, 7L, 6L, 5L, 1L, 9L), .Label = c("$1,324.00", "$102,216.00", 
"$125,101.00", "$198,769.00", "$2,088.00", "$20,070.00", "$21,094.00", 
"$243.91", "$997.00"), class = "factor")), .Names = c("Type", 
"X2011", "X2012", "X2013"), class = "data.frame", row.names = c(NA, 
-9L)) 

代碼:

所有的
colnames(DF)<-c("Type","2011","2012","2013") 
dfMelt<-melt(DF, id.var="Type") 
graph<- ggplot(dfMelt,aes(x=Type, y=value))+ 
geom_bar(aes(fill=variable),stat="identity", position="dodge",linetype=1,colour="red")+ 

#Tried this for black and white-Seems not working 
#scale_colour_grey(start = 0, end = .9) + 

theme_bw()+ 
theme(panel.background = element_rect(fill="grey98"))+ 
theme(axis.text.x = element_text(angle = 45, hjust = 1))+ 

theme(axis.title.x=element_text(size=14,face="bold",vjust=-0.2), 
     axis.title.y=element_text(size=14,face="bold",vjust=0.15))+ 
theme(axis.ticks.x = element_line(size = 2))+ 

scale_x_discrete(expand=c(0.01,0))+ 
scale_y_discrete(expand=c(0.004,0.5)) 


print(graph) 
+0

你需要對你想獲得比「比我在着色方面做了設計,甚至黑白更好地呈現什麼更清晰」。 「更好的介紹」是非常主觀的。 – Henrik

+0

我認爲這個問題很明顯,這就是我得到答案的原因。我試圖得到不同的想法!我把它記下來了。 – SimpleNEasy

+0

首先,我想我已經提供了正在工作的代碼,我沒有毫不費力地問這個問題。正在徵求建議!其次,如果簡單N對你來說很簡單,這並不意味着對其他人來說很容易。 – SimpleNEasy

回答

3

你的價值觀被視爲因素,而不是數字,所以這個表是沒有意義的。因此,首先要將其轉換爲數值:

DF <- cbind(DF[1],sapply(DF[-1], function(x) as.numeric(gsub("[$,]","",x)))) 

然後你就可以像以前一樣繼續進行,但明顯改變y軸上的離散規模擴張到連續一個也格式化值美元和使用藍調布魯爾調色板與scale_fill_brewer它運作良好的黑色和白色和彩色。您也可以在設置調色板時設置圖例標題。

dfMelt<-melt(DF, id.var="Type") 
graph<- ggplot(dfMelt,aes(x=Type, y=value))+ 
geom_bar(aes(fill=variable),stat="identity", position="dodge",linetype=1,colour="red")+ 
theme_bw()+ 
theme(panel.background = element_rect(fill="grey98"))+ 
theme(axis.text.x = element_text(angle = 45, hjust = 1))+ 

theme(axis.title.x=element_text(size=14,face="bold",vjust=-0.2), 
     axis.title.y=element_text(size=14,face="bold",vjust=0.15))+ 
theme(axis.ticks.x = element_line(size = 2))+ 

scale_x_discrete(expand=c(0.01,0))+ 
scale_y_continuous("Price",labels=dollar)+ 

scale_fill_brewer("Year", palette="Blues") 

其中給出:

barchart

+0

我認爲數據中的問題是因爲我從csv文件讀取並使用標準的read.csv。任何其他閱讀方式? – SimpleNEasy

0

首先,你的數據是不正確的格式。現在它是一個因子變量,它需要是數字。此外,刪除逗號(數千)和$ valuta-sign。我也清理了ggplot的代碼。

DF <- cbind(DF[1],sapply(DF[-1], function(x) as.numeric(gsub("[$,]","",x)))) # copied from James 
colnames(DF)<-c("Type","2011","2012","2013") 
dfMelt <- melt(DF, id.var="Type") 

ggplot(dfMelt,aes(x=Type, y=value)) + 
    geom_bar(aes(fill=variable),stat="identity", position="dodge") + 
    theme_bw() + 
    theme(panel.background = element_rect(fill="grey98"), 
     axis.text.x = element_text(angle = 45, hjust = 1), 
     axis.title.x=element_text(size=14,face="bold",vjust=-0.2), 
     axis.title.y=element_text(size=14,face="bold",vjust=0.15), 
     axis.ticks.x = element_line(size = 2)) + 
    scale_y_continuous("Price (in dollars)") + 
    scale_fill_discrete("Year") 

結果: enter image description here