title font size in ggplot2 does not change - r

I know this is a very basic question, but I have trouble changing font size of axis labels in ggplot2. I used the code like below:
a <- ggplot(data1, aes(x=data2, y=data3)) +
geom_hline(yintercept=c(1, -1)) +
labs(x = data2, y = data3) +
theme_bw() +
theme(axis.text=element_text(size=10))
I tried to change the axis label size but they do not change.... Does anybody have suggestion?

Check out here how to alter labels in ggplot:
http://www.sthda.com/english/wiki/ggplot2-title-main-axis-and-legend-titles
In the example below we use axis.title to change the size, colour and face of the text.
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) +
geom_point() +
theme_bw() +
theme(axis.title=element_text(size=10, colour = "red", face = "bold"))

Related

Edit distance between the facet / strip and the plot

For example,
library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(drv))
How can I change the distance between the strip and the main plot? (For example, create a gap between the strip and the main plot.)
But I don't need to change the strip size (different from this edit strip size ggplot2).
There can be multiple solutions to this problem.
geom_hline
A hacky one is to add a line (probably white, but it depends on your theme) on top of the plot. We can do this using geom_hline (or geom_vline if your facets are in rows). This creates an illusion of distance.
library(ggplot2)
ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(cols = vars(drv)) +
# Add white line on top (Inf) of the plot (ie, betweem plot and facet)
geom_hline(yintercept = Inf, color = "white", size = 4) +
labs(title = "geom_hline")
strip.background
Another solution (as suggested by #atsyplenkov) is to use theme(strip.background = ...). There you can specify color of the border. However, this is not a perfect as it cuts border from all the directions (there might be a way to improve this).
ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(cols = vars(drv)) +
# Increase size of the border
theme(strip.background = element_rect(color = "white", size = 3)) +
labs(title = "strip.background")
There is a much simpler solution
theme(strip.placement = "outside")

I want to change the color of ggplot bar charts

I want to change the color of ggplot bar charts manually using the scale_color_manual function. Here is the code:
library(ggplot2)
ggplot(UM.Leads, aes(Leads, Count, fill = Model)) +
geom_bar(stat = "identity") +
xlab("Electrode Model") +
ylab("DBS Leads") +
ggtitle("University of Minnesota") +
scale_color_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()
I cannot seem to change the fill of the bar graphs from the default pink, green, and blue that ggplot provides. Any help would be much appreciated!
See plot here: http://rpubs.com/Gopher16/393415
To illustrate the comment from #Jack Brookes and create a reproducible example:
library(ggplot2)
df <- data.frame(
gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30)
)
ggplot(df, aes(gp, y, fill=gp)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()

Bar plot options with ggplot2

I am actually trying to do a graph with ggplot2 but I'd like to add some options (colors, legend...).
Here is my code :
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment)) +
stat_summary(fun.y="mean", geom="bar") +
facet_grid(. ~ treatment) +
theme_grey() +
xlab("Treatment") +
ylab("OT") +
scale_fill_grey() +
theme(strip.background = element_rect(colour = "black", fill = "white"))
And here the actual output.
Could you please indicate me how to change the name of 1 and 2 (without changing in it the dataframe) and how to add colours to this ?
I tried this
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, colour=Treatment))
But it applies the color only to the outline of the figure.
To change the color of the bars you need fill = Treatment.
To change the labels on the x axis you need scale_x_discrete(labels = your_labels). See here.
So your code will look like:
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, fill= Treatment)) +
scale_x_discrete(labels = your_labels) +
...

Add ylab to ggplot with fivethirtyeight ggtheme

Is it possible to add a label to the y axis if you are using theme_fivethirtyeight? I tried ylab but it does not work:
library(ggplot2)
library(ggthemes)
p2 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) +
geom_point() +
ggtitle("Cars")
p2 + geom_smooth(method = "lm", se = FALSE) +
scale_color_fivethirtyeight("cyl") +
theme_fivethirtyeight() + ylab('SOMETHING')
You can, but it'll take a bit more work than ylab because you need to change some of the theme settings that are the defaults in theme_fivethirtyeight. If you take a look at the code for theme_fivethirtyeight (just run theme_fivethirtyeight in your console to see the code), you'll see that axis.title is set to element_blank(). So this theme has no axis titles at all. You'll need to change this if you want to set a y axis label.
For example, you could add
theme(axis.title = element_text()) + ylab('Something')
to your graph, but then you'll get an x axis label, as well.
An alternative would be to use
theme(axis.title = element_text(), axis.title.x = element_blank()) + ylab('Something')
Asaxis.title.y inherits from axis.title, it didn't work to just set axis.title.y to element_text().

How does one move the tick labels closer to the axis?

library(ggplot2)
p <- ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point()
p + ylab("weight (lb)") +theme_bw()
I would like to move 5000, 4000, 3000, and 2000 closer to the vertical axis. I know one can instead use theme(axis.title.y=element_text(vjust=0.36,hjust=.36)) or similar to move the axis title further away, but sometimes I really want to move the tick labels, not the axis title.
Version 2.0.0 introduced the new margin() which we can use here:
ggplot(mtcars, aes(x = mpg, y = wt*1000, color = factor(cyl))) +
geom_point() +
ylab("weight (lb)") +
theme_bw() +
theme(axis.text.y = element_text(margin = margin(r = 0)))
My reading of this issue on github is, that you should use vjust only for the y-axis and hjust only for the x-axis. To alter the distance between tick-label and axis, use margin(r = x) on the y-axis, and margin(t = x) on the x-axis. Doc for element_text reads: "When creating a theme, the margins should be placed on the side of the text facing towards the center of the plot."
One solution would be to use axis.ticks.margin= element of theme() and set it to 0. But this will influence both axis.
library(ggplot2)
library(grid)
ggplot(mtcars, aes(x=mpg, y=wt*1000, color = factor(cyl))) + geom_point() +
ylab("weight (lb)") +theme_bw()+
theme(axis.ticks.margin=unit(0,'cm'))

Resources