R ggplot2条形图

R ggplot2条形图,r,ggplot2,R,Ggplot2,我有一个小data.frame,我设法在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", "Typ

我有一个小data.frame,我设法在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)

colnames(DF)首先,您的数据格式不正确。现在它是一个因子变量,需要是数字。此外,删除逗号(对于千)和
$
valuta符号。我还清理了
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")

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

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


你需要更清楚地说明你希望实现的目标,而不是“在色彩和设计方面,甚至在黑白方面,比我做的更好”。“更好的陈述”是非常主观的。我认为问题很清楚,这就是我得到答案的原因。我试图得到不同的想法!!我记下来了。首先,我想我已经提供了有效的代码,我没有不遗余力地问这个问题。我在征求建议!!!第二,如果它对你来说很简单,并不意味着对其他人来说很容易。我认为数据中的问题是因为我从csv文件中读取数据,并使用标准的read.csv。还有其他的阅读方式吗?
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")