Genomics-CRT / Data-Science-For-Life-Science

CRT outreach program to assist life scientists in learning basic data science skills.
28 stars 44 forks source link

Setting limits on chart axes #17

Closed gmca22 closed 4 years ago

gmca22 commented 4 years ago

I am having some difficulty with the scales on the barchart. I either have a gap between 0 on the Y-axis and the bars themselves, or end up with a slightly truncated graph on the RHS, at 1200. I am applying the limits incorrectly on the x-axis, but I don't know how to fix it.

#add theme, with a darker background there's now a visible gap between Y-axis and 0 
ggplot(data = pbmc_data, aes(y = cell_type, fill = cell_type)) + 
  geom_bar(show.legend = F) + scale_fill_manual(values = cell_colours) + 
  scale_y_discrete(limits = rev_names) + labs(x = "Number of Cells", y = NULL) + 
  theme_linedraw()

#remove gap between Y axis and 0 using scale_y_discrete and set the limit on X-axis
ggplot(data = pbmc_data, aes(y = cell_type, fill = cell_type)) + 
  geom_bar(show.legend = F) + scale_fill_manual(values = cell_colours) + 
  scale_y_discrete(limits = rev_names) + labs(x = "Number of Cells", y = NULL) + 
  theme_linedraw() + scale_x_discrete(limits = seq(0, 1200, 300))

I don't really understand the function of the "scale_x_discrete(limits = seq(0, 1200, 300))" other than to change the axis scale increment. For example, how would I increase the scale on the X axis to give a number larger than my data e.g. 1500

Sarah145 commented 4 years ago

Hi,

Great question! You can remove the gap between the y axis and 0 using the expand attribute of the x axis and control the limits using the limits attribute. In this case your x axis is continuous rather than discrete so you can use scale_x_continuous() like so:

ggplot(data = pbmc_data, aes(y = cell_type, fill = cell_type)) + 
  geom_bar(show.legend = F) + 
  scale_fill_manual(values = cell_colours) + 
  scale_y_discrete(limits = rev_names) + 
  scale_x_continuous(expand = c(0, 0), limits = c(0, 1200)) +
  labs(x = "Number of Cells", y = NULL) + 
  theme_linedraw()

However, as you've mentioned, this can result in a slightly truncated graph at the RHS. You can use theme() to set the plot margins to prevent the graph from getting cut off like so:

ggplot(data = pbmc_data, aes(y = cell_type, fill = cell_type)) + 
  geom_bar(show.legend = F) + 
  scale_fill_manual(values = cell_colours) + 
  scale_y_discrete(limits = rev_names) + 
  scale_x_continuous(expand = c(0, 0), limits = c(0, 1200)) +
  labs(x = "Number of Cells", y = NULL) + 
  theme_linedraw() +
  theme(plot.margin = margin(10, 25, 10, 10))

Hope this helps!

Sarah

gmca22 commented 4 years ago

That's great - thanks so much. I was trying to use scale_x_continuous(limits = seq(0, 1200, 300) and it was just removing any bar with a value of >300

This makes more sense now, thanks again!