2

I'm trying to plot negative and positive values on x-axis and y-axis to see the distribution. However, ggplot2 plots all the values on side of the axis, even the negative ones. I want to make something like this but with dots on it denoting distribution based on my data: How should I do this?

enter image description here

Here's a sample dataset:

df <- structure(list(States = structure(c(1L, 3L, 2L, 4L), 
                                    .Label = c("AP", "Gujarat", "Punjab", "Rajasthan"), 
                                    class = "factor"), 
                 a = c(20, 45, -15, 10), 
                 b = c(14, -67, 45, -5)), 
            class = "data.frame", 
            row.names = c(NA, -4L))

Here's my code:

library(tidyverse)
library(ggplot2)

df1 <- df

p <- ggplot(df1, aes(a, b), scale="globalminmax") +
  geom_point() +
  theme_minimal()

p
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Shreya Agarwal
  • 676
  • 2
  • 8
  • 20
  • Possible duplicate of [Moving x or y axis together with tick labels to the middle of a single ggplot (no facets)](https://stackoverflow.com/questions/39071002/moving-x-or-y-axis-together-with-tick-labels-to-the-middle-of-a-single-ggplot-n) – Mojoesque Sep 05 '19 at 09:23
  • I'm not entirely sure what the issue is. The points aren't made in the wrong places; is the problem actually that the axis lines are drawn at the bottom and left sides of the panel, whereas you want them drawn at x = 0 and y = 0? – camille Sep 05 '19 at 13:22

1 Answers1

3

A workaround is adding a vertical and horizontal line at 0

ggplot(df1, aes(a, b), scale="globalminmax") +
  geom_vline(xintercept = 0, linetype = 2) +
  geom_hline(yintercept = 0, linetype = 2) +
  geom_point() +
  theme_minimal()
Thierry
  • 18,049
  • 5
  • 48
  • 66