I'm trying to create a graph using ggplot, and have added in the legend.position="top"
function into the theme section.
However this causes the end of my x-axis to be cut off (labels)
Just wondering if anyone knows if there is an easy fix for this?
Thanks
EDIT Dataframe (I just used a set of 100 randomly generated numbers)
geom_histogram(breaks=c(seq(0, 25, by=5)),col="white")
theme_light() #removes background colour
labs(x="Length in Cm", y="Frequency", )
scale_x_continuous(limits=c(0,25), breaks=c(seq(0, 25, by=5)), labels=c(seq(0,25, by=5)), expand = c(0,0))
scale_y_continuous(expand = c(0,0), limits = c(0,25), breaks = c(seq(0,25,by=5)))
scale_fill_manual(values = c("#41ae76", "#006d2c"), name="")
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),legend.title = element_text(size = 20),
legend.text = element_text(size = 16),panel.background = element_blank(), axis.line = element_line(colour = "black"),
axis.text = element_text(size = 16),legend.position = "top", axis.title = element_text(size = 20),legend.key=element_rect(fill="white"))```
CodePudding user response:
I suggest simplifying your plotting routine (there are quite a few redundant & unnecessary statements).
df %>%
ggplot(aes(Length, fill = Group))
geom_histogram(breaks = seq(0, 25, by = 5), col = "white")
theme_light()
labs(x = "Length in Cm", y = "Frequency")
scale_x_continuous(limits = c(0, 25))
scale_y_continuous(limits = c(0, 25))
scale_fill_manual(values = c("#41ae76", "#006d2c"), name = "")
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_text(size = 20),
legend.text = element_text(size = 16),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
axis.text = element_text(size = 16),
legend.position = "top",
axis.title = element_text(size = 20),
legend.key=element_rect(fill = "white"))
This produces
Some selective & further comments:
Remove the
expand
arguments ofscale_*_continous
. They are likely to be responsible for cutting off text.No need for duplicating
breaks
insidegeom_histogram
andscale_x_continuous
.The
breaks
argument toscale_y_continous
is unnecessary.There are lots of
theme
alterations totheme_light()
. Perhaps better start with a more minimalistictheme_minimal()
which will require lesstheme
alterations.I would strongly recommend using a consistent code indentation & formatting style to help with readability.
Sample data
df <- structure(list(
Length = c(19, 23, 19, 21, 21, 21, 2, 17, 1, 20,
14, 12, 5, 17, 13, 14, 14, 6, 18, 11, 8, 20, 2, 5, 17, 23, 8,
1, 11, 11, 15, 11, 7, 23, 21, 3, 11, 9, 6, 8, 5, 21, 10, 17,
24, 12, 11, 3, 16, 5, 10, 12, 12, 23, 19, 25, 17, 7, 10, 25,
5, 21, 12, 7, 10, 17, 1, 25, 11, 15, 25, 14, 13, 2, 1, 6, 1,
24, 13, 17, 7, 25, 19, 17, 2, 5, 5, 23, 3, 17, 21, 8, 2, 8, 13,
1, 4, 2, 20, 20),
Group = c("a", "b", "a", "b", "a", "b", "a",
"b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
"a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
"b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
"a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
"b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
"a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
"b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
"a", "b")),
row.names = c(NA, -100L),
class = c("tbl_df", "tbl",
"data.frame"))