Meems
Meems

Reputation: 101

Changing x axis on my plot to fit in one more value: xlim, scale_x_continuous not working

I created a cleveland dot plot for my data, but it stops a bit shorter than the last data point. enter image description here

I would like it to end on 7000. I tried to use xlim(1000,7000) and scale_x_continuous(breaks = seq(1000, 7000, by = 1000) but it doesnt work. My code:

ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
     geom_point() + geom_line(aes(group = Trio_number), color = 'grey30') +
     scale_y_continuous(breaks = seq(0, 20, by = 1)) + scale_x_continuous(breaks = seq(1000, 
     7000, by = 1000) +
     ylab("Trio number") + xlab("Genome size (kb)") + theme_dotplot +  theme(legend.position = 
     "bottom") + scale_color_brewer(palette = "Set2") + theme(legend.title=element_blank()) + 
     guides(colour = guide_legend(override.aes = list(size=4))) +
     theme(legend.key=element_rect(fill='grey96')) +
     theme(plot.background = element_rect(fill = 'grey96')) + theme(legend.title = 
     element_text(size=10)) +  theme(text=element_text(size=12,  family="Gujarati Sangam MN")) 
     + theme(axis.title.x = element_text(vjust = 0, size = 12), axis.title.y = 
     element_text(vjust = 2, size = 12)) + theme(axis.text = element_text(color = "black", 
     size = 9))

Reproducible data:

library(ggplot2)
library(scales)

set.seed(8675309)

tidydf <- data.frame(
  Genome_size = sample(1000:7000, 30, replace = T),
  Trio_number = sample(1:20, 30, replace = T),
  Group = sample(c('Free-living', 'Gut', 'Pathogen'), 30, replace = T)
)

p <- 
ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
  geom_point() +
  scale_y_continuous(breaks = seq(0, 20, by = 1)) +
  ylab("Trio number") + xlab("Genome size (kb)") +
  theme_light() +
  scale_x_continuous(labels = comma) +
  scale_color_brewer(palette = "Accent") +
  guides(colour = guide_legend(override.aes = list(size=4))) +
  theme(
    legend.position = "bottom",
    legend.key=element_rect(fill='gray96'),
    plot.background = element_rect(fill = 'gray96'),
    legend.title =element_text(size=10),
    text=element_text(size=12),
    axis.title.x = element_text(vjust = 0, size = 11),
    axis.title.y = element_text(vjust = 2, size = 11),
    axis.text = element_text(color = "black", size = 9),
    
    # to make the theme look more similar to OP example
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()
  )
p

Thanks!

Upvotes: 0

Views: 98

Answers (1)

Andy Baxter
Andy Baxter

Reputation: 7636

I think it would work by adding both limits = c(1000, 7000) and breaks = seq(1000, 7000, by = 1000) inside the scale_x_continuous call:

library(ggplot2)
library(scales)

set.seed(8675309)

tidydf <- data.frame(
  Genome_size = sample(1200:6800, 30, replace = T),
  Trio_number = sample(1:20, 30, replace = T),
  Group = sample(c('Free-living', 'Gut', 'Pathogen'), 30, replace = T)
)

  ggplot(tidydf, aes(Genome_size, `Trio_number`, color = Group)) +
  geom_point() +
    geom_line(aes(group = Trio_number), color = 'grey30') +
  scale_y_continuous(breaks = seq(0, 20, by = 1)) +
  ylab("Trio number") + xlab("Genome size (kb)") +
  theme_light() +
  scale_x_continuous(labels = comma,
                     limits = c(1000, 7000),
                     breaks = seq(1000, 7000, by = 1000)) +
  scale_color_brewer(palette = "Accent") +
  guides(colour = guide_legend(override.aes = list(size=4))) +
  theme(
    legend.position = "bottom",
    legend.key=element_rect(fill='gray96'),
    plot.background = element_rect(fill = 'gray96'),
    legend.title =element_text(size=10),
    text=element_text(size=12),
    axis.title.x = element_text(vjust = 0, size = 11),
    axis.title.y = element_text(vjust = 2, size = 11),
    axis.text = element_text(color = "black", size = 9),
    
    # to make the theme look more similar to OP example
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()
  )

Created on 2021-03-22 by the reprex package (v1.0.0)

Upvotes: 1

Related Questions