ggplot2 barplot

时间:2014-04-07 12:19:08

标签: r ggplot2

我有一个小数据框架,我设法在ggpot中绘图。由于ggplot不支持模式,我用颜色绘制数据。我很欣赏比我在着色和设计方面做得更好的演示,甚至是黑白。另外,我无法更改图例标题

我的数据:

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)

2 个答案:

答案 0 :(得分:3)

您的价值被视为因素而不是数字,因此图表没有意义。首先,您要将它们转换为数值:

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

然后您可以像以前一样继续进行,但显然可以将y轴上的离散比例扩展更改为连续的扩展,也可以将值格式化为美元,并使用带有scale_fill_brewer的Blues 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

答案 1 :(得分: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