1

I am making a geom_col in ggplot2. The x-axis is a numerical vector of timepoints (0, 6, 18, 24, 32, 44). There is a difference between each column corresponding to the numerical difference between each timepoint. But i want an equal distance between all the columns. I have searched for answers in here, but i didn't find a similar issue.

This is my code:

ggplot(data = ny_dataframe_scratch, aes(x=timepoint, y = relative_wound_healing, fill = Condition)) +
  geom_col(width = 5, position = position_dodge()) + 
  scale_x_continuous(breaks=c(0, 6, 18, 24, 32, 44), name = "Time point, hours") + 
  scale_y_continuous(name = "Relative scratch area") + 
  scale_fill_manual(values=c("palevioletred4", "slategray")) +
  geom_point(data = ny_dataframe_scratch, position = position_dodge(width = 5), aes(x=timepoint, y=relative_wound_healing, fill = Condition))

This is the output of dput():

structure(list(timepoint = c(0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 
6, 18, 18, 18, 18, 18, 18, 24, 24, 24, 24, 24, 24, 32, 32, 32, 
32, 32, 32, 44, 44, 44, 44, 44, 44), Condition = structure(c(2L, 
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
1L, 1L, 1L), .Label = c("Control", "Knockout"), class = "factor"), 
    relative_wound_healing = c(1, 1, 1, 1, 1, 1, 0.819981, 0.78227, 
    0.811902, 0.873852, 0.893572, 0.910596, 0.39819, 0.436948, 
    0.559486, 0.534719, 0.591295, 0.612154, 0.222731, 0.2592, 
    0.453575, 0.37238, 0.477891, 0.505393, 0.05243246, 0.0809449, 
    0.2108063, 0.261122, 0.3750218, 0.4129873, 0, 0.0240122, 
    0.0778219, 0.0806758, 0.2495444, 0.3203724)), class = "data.frame", row.names = c(NA, 
-36L))

Picture of how the graph looks:

  • Unrelated to the issue you’re having, this is a very misleading graph: usually when you plot bars for multiple observations the bar height corresponds to the mean (or the median); in your case it corresponds to the maximum. – Konrad Rudolph Apr 11 '21 at 13:36
  • Thank you Konrad, you are right. That is another issue, that i am trying to solve. I want to have the bars height corresponding to the mean of the three values. But I currently can't get that to work out for me. – Sofie Lindholm Apr 11 '21 at 13:38
  • Could you post the `dput()` output of your data.frame? – Konrad Rudolph Apr 11 '21 at 13:44
  • https://ibb.co/G95hYrS Sorry, i am new on stackoverflow. The output is too many characters to be posted in a comment, so this is a screenshot instead. – Sofie Lindholm Apr 11 '21 at 13:51
  • Please edit your question to add the data. A screenshot isn’t very helpful since I can’t paste data into R. ;-) – Konrad Rudolph Apr 11 '21 at 13:55
  • 1
    Oh yes, sure! It is included in my post now! – Sofie Lindholm Apr 11 '21 at 13:59

2 Answers2

1

The x-scale has proportional gaps because ‘ggplot2’ considers the values as continuous rather than categorical.

To make it categorical, you can for instance use factors:

aes(x = factor(timepoint, ordered = TRUE), …

(Without ordered = TRUE, ‘ggplot2’ assumes alphabetical ordering, so it would put 11 before 5, which probably isn’t what you want.)

To fix the bar heights, you need to compute and plot a summary statistic — ‘ggplot2’ allows you to do this using stat_summary (instead of geom_col):

stat_summary(fun.y = mean, geom = "col", position = position_dodge())

Taken together:

ggplot(ny_dataframe_scratch) +
    aes(x = factor(timepoint, ordered = TRUE), y = relative_wound_healing, fill = Condition) +
    scale_fill_manual(values = c("palevioletred4", "slategray")) +
    stat_summary(fun.y = mean, geom = "col", position = position_dodge()) +
    geom_point(position = position_dodge(width = 1)) +
    labs(x = "Time point, hours", y = "Relative scratch area")
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

Your timepoints are "numeric". Try coercing them to factor. At that point, ggplot should plot them at equidistance from each other.

xy$timepoint <- as.factor(xy$timepoint)
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • Thank you! Then i get this error message: "Discrete value supplied to continuous scale" – Sofie Lindholm Apr 11 '21 at 14:09
  • @SofieLindholm you will need to iron out the details. In your case, you have a `scale_x_continuous` which becomes obsolete since you've just coerced that variable to a factor. – Roman Luštrik Apr 12 '21 at 08:20