french_fries
french_fries

Reputation: 1

Split ticks on y axis

I have drawn a heatmap, but it looks like this:

enter image description here

As you see all ticks on y axis are covering each other and all in all it doesn't look good. How could I separate these ticks from each other on y axis? My code is:

ggplot(df, aes(Date, Place`)) +
  geom_tile(aes(fill = N)) +
  scale_fill_viridis(name = N, label = comma) +
  theme_tufte(base_family = "Helvetica") +
  theme(axis.ticks = element_blank()) +
  theme(axis.text = element_text(size = 10)) +
  scale_y_discrete(expand=c(0.2,0))

I tried to change scale_y_discrete(expand=c(0.2,0)) to scale_y_discrete(expand=c(0.7,0)), but nothing changed. i can't decrease number of objects on y axis

Data sample:

Date        Place          N
2020.04.20  8797    173032
2020.05.01  315D    10
2020.04.13  Q168    193597
2020.04.19  8797    96104
2020.04.02  8797    244935
2020.04.04  315D    474049
2020.05.01  8797    13
2020.04.23  315D    125607
2020.04.18  Q168    787224
2020.04.11  8797    282303
2020.04.12  8797    138443
2020.04.24  Q168    176487
2020.03.19  315D    290053
2020.04.10  315D    561935
2020.04.06  Q168    221196
2020.03.26  Q168    202552
2020.03.23  315D    516936
2020.04.06  315D    195038

Upvotes: 0

Views: 111

Answers (2)

teunbrand
teunbrand

Reputation: 37913

Here are two solutions adjusting the y-axis guide. I'm sorry for not using your data, I couldn't easily paste it into my R session.

The first option is to not display labels that are overlapping. You can do this with setting check.overlap = TRUE in the axis guide.

library(ggplot2)

ggplot(iris, aes(Petal.Width, paste0(Species, "_", 1:150))) +
  geom_point() +
  guides(y = guide_axis(check.overlap = TRUE))

The second option is to 'dodge' the labels, i.e. place labels in a hierarchy, the depth of which is controlled by n.dodge.


ggplot(iris, aes(Petal.Width, paste0(Species, "_", 1:150))) +
  geom_point() +
  guides(y = guide_axis(n.dodge = 2))

Created on 2020-05-04 by the reprex package (v0.3.0)

Upvotes: 0

Jeff Bezos
Jeff Bezos

Reputation: 2253

Are you planning to export your data? If so, I like to fix any scaling issues at that step.

ggsave(my_plot, filename = "plot.pdf", scale = 0.5)
ggsave(my_plot, filename = "plot.pdf", width = 10, height = 10, units = "in")

You can replace "pdf" with pretty much any file format.

Upvotes: 1

Related Questions