jimac82
jimac82

Reputation: 21

Changing width of just one column in geom_col

I have a dataset with most values very close to 0, and one value closer to 6. I am asked for a bar plot of each value. I have roughly 4000 observations. With so many observations, geom_col can't seem to fit them all in the plot:

test <- data.frame(obs = 1:5000, value = abs(rnorm(5000, 0, .001)))
test[2500, 'value'] <- test[2500, 'value'] + 6
ggplot(test, aes(obs, value)) +
  geom_col() +
  theme_bw()
ggplot(test[2400:2600,], aes(obs, value)) +
  geom_col() +
  theme_bw()

enter image description here

If I narrow the number of observations graphed, my single large value is plotted:

enter image description here

Is it possible to change the thickness of just the single large observation so I can still display the full range of data?

Upvotes: 0

Views: 349

Answers (1)

stefan
stefan

Reputation: 125208

Instead of relying on geom_col or trying some hacks via geom_rect to change the width of the plot you can simply use geom_line and optionally add a geom_point to make a dot or lollipop plot which is basically the same as a barplot. Try this

BTW: Maybe using a log-scale is also a good option.

set.seed(42)

library(ggplot2)

test <- data.frame(obs = 1:5000, value = abs(rnorm(5000, 0, .001)))
test[2500, 'value'] <- test[2500, 'value'] + 6

ggplot(test, aes(obs, value, color = value)) +
  geom_line() +
  geom_point() +
  theme_bw()

Created on 2020-06-09 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions