我正在尝试使用超过400,000个数据点(由三列组成)绘制条形图:成本,排名和年份,但是当我绘制facetwrap图表时,会在每个条形图中获得这些水平线。有人可以告诉我为什么他们在那里以及如何摆脱它们。我正在使用的代码是:
library(ggplot2)
costs<-read.table("C:/Projects/cost_rank_1.txt",header=TRUE)
df<-data.frame(costs)
ggplot(df,aes(x=rank, y=cost)) +
geom_bar(position=position_dodge(),
stat="identity",colour="steelblue",fill="cornsilk3")+
facet_wrap(~year) +
scale_x_continuous(breaks = seq(1,21,by = 2),
labels =
c("1","3","5","7","9","11","13","15","17","19","21")) +
scale_y_continuous(breaks=c(0,500,1000,1500,2000,2500))+
labs(x="Rank",y="Average Cost per Rank (dollars)")+ggtitle("All Tiers") +
theme(plot.title = element_text(color="black", size=14, face="bold"))
图表如下:
答案 0 :(得分:1)
可以通过修改aes()
and geom_bar()
components解决此问题。具体来说,您必须将y=cost
更改为fill=cost
,并将stat="identity"
更改为stat="count"
,从而得到以下代码:
library(ggplot2)
costs<-read.table("C:/Projects/cost_rank_1.txt",header=TRUE)
df<-data.frame(costs)
ggplot(df,aes(x=rank, fill=cost)) +
geom_bar(position=position_dodge(),
stat="count",colour="steelblue",fill="cornsilk3")+
facet_wrap(~year) +
scale_x_continuous(breaks = seq(1,21,by = 2),
labels =
c("1","3","5","7","9","11","13","15","17","19","21")) +
scale_y_continuous(breaks=c(0,500,1000,1500,2000,2500))+
labs(x="Rank",y="Average Cost per Rank (dollars)")+ggtitle("All Tiers") +
theme(plot.title = element_text(color="black", size=14, face="bold"))
P.S。尝试提供完全可重复的案例,包括数据。