DaltonicD
DaltonicD

Reputation: 75

Changing y-axis line to where x = 0 with ggplot 2 in R

I have this graph where the only thing I want to change is the position of the line of the y-axis, so that it matches the gridline defined by x=0%. I do not want to change the position of the numbers of the y-axis label. How do I go about this? Thank you in advance!


My code: (dput(data_us) at the bottom of this question)

plot <- ggplot(data=data_us, aes(x=BoPbyGDP, y=GDP_pc)) + 
        geom_point(size=3, color="blue", alpha=0.7) +
        theme_light() +
        ggtitle("GDP per capita and Balance of Payments by State") +
        xlab("BoP (% state GDP)") +
        ylab("GDP per capita ($)") +
        theme(
          axis.title.x = element_text(size=12, face="bold",
                                      margin = margin(t = 7, b = 5)),
          axis.title.y = element_text(size=12, face="bold",
                                      margin = margin(r = 11, l = 5)),

          axis.text.x = element_text(size=11, face="bold"),
          axis.text.y = element_text(size=10, face="bold"),

          axis.line.x = element_line(color="black"),
          axis.line.y = element_line(color="black"),

          plot.title = element_text(size=15, face="bold",
                                    margin = margin(t = 5, b = 15))
        ) +
        scale_x_continuous(breaks=seq(-0.1, 0.25, by=0.05), 
                           labels = scales::percent_format(accuracy = 1)) +
        scale_y_continuous(limits=c(35000, 80000), expand=c(0,0))

Graph as it currently is: Graph as it currently is

dput(data_us):
structure(list(GDP_pc = c(42393.89742, 76313.08356, 45505.0029, 
41144.53056, 71012.64198, 62873.02224, 73008.2031, 70199.00118, 
45763.01268, 53125.39134, 60666.55686, 42268.63956, 63138.52608, 
51895.29492, 57399.1467, 55748.31234, 44374.47042, 53035.46262, 
45073.55916, 63985.35486, 76603.21074, 49286.29146, 62571.11868, 
36366.53202, 48770.2719, 45803.69472, 63202.76088, 51812.86026, 
58830.51216, 65487.3786, 46750.08744, 77546.39172, 50619.16356, 
72343.37292, 54569.60376, 52161.86934, 53902.63242, 58074.68268, 
53737.7631, 43041.59832, 55951.72254, 50081.7324, 62288.48556, 
52865.2404, 50424.318, 58793.04186, 69768.62802, 41187.35376, 
54480.74562, 70265.37714), BoPbyGDP = c(0.155107042826638, 0.100650347179995, 
0.0949172477937122, 0.123696032925093, 0.000161398415464849, 
-0.00152283799860802, -0.0534939929671034, 0.0393077762141327, 
0.0465533232677134, 0.0414882529388836, 0.0841338761854389, 0.0809370324738185, 
-0.00562880963617377, 0.0447927916843252, 0.0191150438584673, 
0.0358269093832264, 0.202939086886208, 0.074120741140523, 0.119970406332658, 
0.0926397039912382, -0.0297252517631743, 0.0487537413867995, 
0.0152269328045785, 0.186267162871042, 0.0791746216217812, 0.0841058808934568, 
-0.00261373046547635, 0.0215237749893609, -0.00388132538609299, 
-0.0358241597649685, 0.192527403516179, -0.0221683530598698, 
0.0640711320322971, -0.0103673747863158, 0.0496834158239401, 
0.0831775656281504, 0.0452918975425262, 0.0395464080470773, 0.0403546284759077, 
0.11278237613458, 0.0246488147056518, 0.0697066452994047, 0.00515960370113009, 
0.00548265937080355, 0.0724313718185149, 0.171293009756312, -0.00260282393058425, 
0.180761014120545, 0.00964011979339583, 0.0103861845056283)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 2

Views: 4416

Answers (1)

bs93
bs93

Reputation: 1316

You can get close by drawing your own axis as stefan talks about. Here, theme_minimal() removes both the x and y axis. Add, x axis with theme(). Make a y-axis with geom_vline().

 ggplot(data=data_us, aes(x=BoPbyGDP, y=GDP_pc)) + 
  geom_point(size=3, color="blue", alpha=0.7) +
  theme_light() +
  ggtitle("GDP per capita and Balance of Payments by State") +
  xlab("BoP (% state GDP)") +
  ylab("GDP per capita ($)") +
  scale_x_continuous(breaks=seq(-0.1, 0.25, by=0.05), 
                     labels = scales::percent_format(accuracy = 1)) +
  scale_y_continuous(limits=c(35000, 80000), expand=c(0,0))+

  theme_minimal()+ #removes axes
  geom_vline(aes(xintercept = 0))+ #add y-axis where you want
  theme( axis.line.x = element_line(color="black"), #add back in x axis 
         axis.title.x = element_text(size=12, face="bold",
                                     margin = margin(t = 7, b = 5)),
         axis.title.y = element_text(size=12, face="bold",
                                     margin = margin(r = 11, l = 5)),
         axis.text.x = element_text(size=11, face="bold"),
         axis.text.y = element_text(size=10, face="bold"),

         plot.title = element_text(size=15, face="bold",
                                   margin = margin(t = 5, b = 15)))

enter image description here

Upvotes: 3

Related Questions