Reputation: 101
My plot:
I'm looking to
My current code is here
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_dotplot + scale_x_continuous(labels = comma) + theme(legend.position = "bottom") + scale_color_brewer(palette = "Accent") + theme(legend.title=element_blank()) + guides(colour = guide_legend(override.aes = list(size=4))) +theme(legend.key=element_rect(fill='gray96')) +
theme(plot.background = element_rect(fill = 'gray96')) + 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 = 11), axis.title.y = element_text(vjust = 2, size = 11)) + theme(axis.text = element_text(color = "black", size = 9))
How can I achieve this? Online guides haven't helped too much!
Upvotes: 0
Views: 924
Reputation: 13893
In the future, please also share your dataset via dput(tidydf)
in your console + pasting the resultant code into your question. This way, I don't have to first show you how to recreate a simulation of your data before suggesting an answer.
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
The main note for your plot is that all theme()
elements can (and really should) be combined into one theme()
call. By convention, I tend to organize my plot code according to generalized groupings to help keep track of things. Here's my suggested way of organizing, but you can keep your own:
scale_
commands and adjustmentslabs(...)
rather than ggtitle()
, ylab()
, xlab()
1. remove the border around the plot
Just access the theme
element panel.border
.
2. make the lines in the background a bit darker
Access the theme
element family panel.grid
. You can choose to access all grid lines, all minor/major grid lines, or dial down to each minor and/or major grid lines for the x and y axes.
Putting those two together, here's what we get:
p + theme(
panel.border = element_blank(),
panel.grid.major.y = element_line(color='gray70')
)
3. increase the space between the lines slightly
There's kind of two ways to do this. I think all you need to do is remove the minor gridlines of the y axis and it makes it look probably how you want it to look:
p + theme(
panel.border = element_blank(),
panel.grid.major.y = element_line(color='gray70'),
panel.grid.minor.y = element_blank()
)
However, if you still want to spread out the y axis lines a bit... well, this is where the only real option here is to understand that the output will depend on your graphics device - and in particular the size of the window. If you made this chart in Excel or some other program like that, you could spread out those lines further by "stretching" the plot in the y direction overall. That's basically what you would need to do here. In this case, I'm saving the plot using the following code:
ggsave('myplot.png', width=4, height=3.5)
If I wanted to "stretch" the lines in the y axis, I would simply save the plot to be "taller":
ggsave('my_stretched_plot.png', width=4, height=5.5)
Upvotes: 1