0

I have tried to make a bar graph in ggplot2. However, the result only shows one value (because there is no another factor in the data) so I only got one bar and then I add a new row which the factor value is 0 to make comparison.

preframeprebar03
Var1 Freq
No  3895

newRow <- data.frame(Var1 ='Yes',Freq='0')
preframeprebar03 <- rbind(preframeprebar03,newRow)
names(preframeprebar03)<-c("Tree.presence","Freq")

plot4 <-ggplot(preframeprebar03,aes(Tree.presence, Freq))
plot4 <- plot4 + 
  geom_bar(stat="identity", width=0.2, colour="black", fill="steelblue", position="dodge")

And the result is

enter image description here

My question is how to make the 0 break to the down side and make the second bar to be close to the xlim?

morgan121
  • 2,213
  • 1
  • 15
  • 33
Will_Joen
  • 1
  • 2
  • Hi, we can't do much with your code if you don't [provide any data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – jay.sf Feb 01 '19 at 01:19
  • Hello and welcome to SO. To help make a reproducible example, you can use `reproduce()` . Instructions are here: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/16532098#16532098) – Ricardo Saporta Feb 01 '19 at 01:32
  • Hi I have modified and make it easier to read, but not sure this would be helpful or not. – Will_Joen Feb 01 '19 at 01:32
  • I did this: `x <- data.frame(Var1=c("Yes","No"), Freq=c(0, 3895))` and then your code did what you want. I just couldn't create your dataframe becuase you havent provided all the data. My plot code was: `ggplot(data=x, aes(Var1, Freq))+geom_bar(stat="identity", width=0.2, colour="black", fill="steelblue", position="dodge")` – morgan121 Feb 01 '19 at 02:15
  • Thanks a lot, I have tried your code, that works and the graph is what I want. I thought I add a new row made the whole graph looks a bit different! – Will_Joen Feb 01 '19 at 02:23
  • not sure I understood your comment exactly, is the problem fixed? Or did you still have issues after adding a new row? – morgan121 Feb 01 '19 at 02:32

0 Answers0