Angus
Angus

Reputation: 355

How do I remove horizontal lines from my bar plot

I'm trying to render a barchart using over 400,000 data points consisting of three columns: cost, rank and year, but when I plot the facetwrap charts I get these horizontal lines within each bar. Can someone tell me why they are there and how to get rid of them. The code I'm using is:

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"))

The chart looks like this:

[1]: https://i.sstatic.net/5nejD.png

Upvotes: 1

Views: 2278

Answers (1)

Pablo Bernabeu
Pablo Bernabeu

Reputation: 262

The problem can be solved by modifying the aes() and geom_bar() components. Specifically, you would have to change y=cost to fill=cost, and also change stat="identity" to stat="count", resulting in the code below:

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. Try to provide fully reproducible cases, including data.

Upvotes: 3

Related Questions