0

I am trying to create 2 ggplot bar graphs for text analysis to compare frequencies as percentages from the dictionary "loughran". Here is my code for one of the graphs. How can I edit my y range so that both graphs start at 0% and end at 100%? This way, it would be much easier to see the differences.

ggplot(loughran_nc) +
  aes(x = fct_reorder(sentiment, perc), y = perc)+
  geom_col()+
  ylab("Percentage") +
  xlab("Sentiment")+
  ggtitle("Sentiment Analysis: Non-Complaints Loughran dictionary")+
  theme(plot.title = element_text(hjust = 0.5))
vicxirra
  • 1
  • 1
  • It would be easier to help if you create a small reproducible example. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Edit your post to include `dput(loughran_nc)` or if the data is too big `dput(head(loughran_nc, 20))` to share first 20 rows. – Ronak Shah May 14 '21 at 01:55
  • `+ coord_cartesian(ylim = c(0,100))` if 100% is coded as 100, otherwise use `c(0,1)`. – Jon Spring May 14 '21 at 06:59
  • 1
    Does this answer your question? [How to set limits for axes in ggplot2 R plots?](https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots) – Elk May 14 '21 at 14:03

2 Answers2

0

you can set limits within coord_cartesian()

Some quick data:

library(tidyverse)
loughran_nc <- data.frame(sentiment = c("words","for","some","data"),perc=c(40,60,20,80))

Then your plot + 1 line:

ggplot(loughran_nc) +
  aes(x = fct_reorder(sentiment, perc), y = perc)+
  geom_col()+
  ylab("Percentage") +
  xlab("Sentiment")+
  ggtitle("Sentiment Analysis: Non-Complaints Loughran dictionary")+
  theme(plot.title = element_text(hjust = 0.5)) +
  coord_cartesian(ylim = c(0,100))

enter image description here

Elk
  • 491
  • 2
  • 9
0

An alternative to coord_cartesian() is to use scale_y_continuous() or ylim().

  • scale_y_continuous() lets you specify all sorts of attributes to the y axis; limits, breaks, name etc (see ?scale_y_continuous). For your example, you can add scale_y_continuous(limits = c(0, 100)) to your code

  • ylim() is simple, and adding ylim(c(0, 100)) would also do the same job

hugh-allan
  • 1,170
  • 5
  • 16