Erik
Erik

Reputation: 123

Removing some tick labels in boxplots in ggplot2

I'm relatively new to ggplot, so I apologize if this is easy, but I couldn't find anything online.

I want to display 29 boxplots (numbered 1.1 to 4.0) next to each other in ggplot2 (which I can do) but once I make the tick label the appropriate size (which I can do), the labels overlap and I only want a few (1.5, 2, 2.5 etc) anyways. How can I remove only some of the tick labels? Also, anyway I can include a blank tick mark at 1.0 so my tick labels are nice, round numbers?

My data is list which I 'melted' since each boxplot has a different number of observations.

My current code:

list = list(data11, data12, ... data39, data40) # Elipse denotes the rest of the sequence
df = melt(list)

ggplot(df, aes(factor(variable), value)) + 
    geom_boxplot(outlier.size=1.5, colour="black") + 
    xlab("Xlabel") +
    ylab("Ylabel") +
    theme_classic() + 
    theme(
        axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12),
        axis.title.x = element_text(size=14),
        axis.title.y = element_text(size=14, angle=90),
        axis.line = element_line(size=0.75)
    )

Upvotes: 4

Views: 8235

Answers (1)

tonytonov
tonytonov

Reputation: 25608

This is not a difficult question indeed. The key idea can be easily found here.

Basically, you are missing a single line of code. Since you did not share a sample of your data (shame on you! see this), I generated some. Here's the solution:

df.so1 <- runif(10); df.so2 <- runif(10); df.so3 <- runif(10)
list.so = list(df.so1, df.so2, df.so3)
df.so = melt(list.so)

ggplot(df.so, aes(factor(L1), value)) + 
  geom_boxplot(outlier.size=1.5, colour="black") + 
  xlab("Xlabel") + ylab("Ylabel") +
  theme_classic() + 
  theme(
    axis.text.x = element_text(size=12),
    axis.text.y = element_text(size=12),
    axis.title.x = element_text(size=14),
    axis.title.y = element_text(size=14, angle=90),
    axis.line = element_line(size=0.75)
  ) + 
  scale_x_discrete(breaks = c(1,3))

Note that you have full control over axis, ticks, tick labels, etc. See ggplot2 documentation for more.

Upd.

Don't forget to check out related questions before posting: bump1, bump2.

Upvotes: 4

Related Questions