Cannot rotate x axis labels in ggplot2 facet graphs in r - r

Can anyone help out with a task?
I've been trying to run a part of code that creates a facet_wrap() graph in r, and it worked very well for my porpouse but the x axis labels are overlapping each other.
I need to rotate them 45 degrees.
I've been trying to run this code for the porpouse and it did create the graph, but the x axis label didn't rotate as I expected.
Here is the code:
for (x in unique(clss_perc_factor7$Horto)){
ggsave(paste('barplot_esp40', x, sep = '_'),
ggplot(clss_perc_factor7[clss_perc_factor7$Horto == x,],
aes(x = Classe,
y = Freq_perc)) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1)) +
theme_bw() +
geom_bar(stat = 'identity',
colour = 'black',
fill = '#66CC99') +
facet_wrap(~ CD_TALHAO) +
labs(x = 'Classe de Espaçamento',
y = 'Nº de Indivíduos (%)',
title = 'Distribuição do Espaçamento em Classes'),
dpi = 'retina',
device = 'png')
}
Given that:
clss_perc_factor7 is the data frame with more then one forest garden ("Horto" - in the loop).
In each forest garden there are different productive units ("CD_TALHAO" - in facet_wrap())
That's why I'm using a for loop to plot the graphs.
OBS: The part theme(plot.title = element_text(hjust = 0.5)... which was supposed to align the title to center isn't working either.
Thanks a lot guys!

Here is a way:
library(ggplot2)
ggplot(mtcars,aes(hp,mpg)) +
geom_point() +
facet_wrap(~cyl) +
theme(strip.text.x = element_text(angle = 45))
Good luck!

Related

Why are colours appearing in the labels of my gganimate sketch?

I have a gganimate sketch in R and I would like to have the percentages of my bar chart appear as labels.
But for some bizarre reason, I am getting seemingly random colours in place of the labels that I'm requesting.
If I run the ggplot part without animating then it's a mess (as it should be), but it's obvious that the percentages are appearing correctly.
Any ideas? The colour codes don't correspond to the colours of the bars which I have chosen separately. The codes displayed also cycle through about half a dozen different codes, at a rate different to the frame rate that I selected. And while the bars are the same height (they grow until they reach the chosen height displayed in the animation) then they display the same code until they stop and it gets frozen.
Code snippet:
df_new <- data.frame(index, rate, year, colour)
df_new$rate_label <- ifelse(round(df_new$rate, 1) %% 1 == 0,
paste0(round(df_new$rate, 1), ".0%"), paste0(round(df_new$rate, 1), "%"))
p <- ggplot(df_new, aes(x = year, y = rate, fill = year)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = colour) +
#geom_text(aes(y = rate, label = paste0(rate, "%")), vjust = -0.7) +
geom_shadowtext(aes(y = rate, label = rate_label),
bg.colour='white',
colour = 'black',
size = 9,
fontface = "bold",
vjust = -0.7,
alpha = 1
) +
coord_cartesian(clip = 'off') +
ggtitle("% population belonging to 'No religion', England and Wales census") +
theme_minimal() +
xlab("") + ylab("") +
theme(legend.position = "none") +
theme(plot.title = element_text(size = 18, face = "bold")) +
theme(axis.text = element_text(size = 14)) +
scale_y_continuous(limits = c(0, 45), breaks = 10*(0:4))
p
p <- p + transition_reveal(index) + view_follow(fixed_y = T)
animate(p, renderer = gifski_renderer(), nframes = 300, fps = frame_rate, height = 500, width = 800,
end_pause = 0)
anim_save("atheism.gif")
I think you have missed some delicate points about ggplot2. I will try my best to describe them to you. First of all, you need to enter the discrete values as factor or integer. So you can use as.factor() before plotting or just factor() in the aesthetic. Also, you should consider rounding the percentages as you wish. Here is an example:
set.seed(2023)
df_new <- data.frame(index=1:10, rate=runif(10), year=2001:2010, colour=1:10)
df_new$rate_label <- ifelse(round(df_new$rate, 1) %% 1 == 0,
paste0(round(df_new$rate, 1), ".0%"),
paste0(round(df_new$rate, 1), "%"))
The ggplot for this data is:
library(ggplot2)
p <- ggplot(df_new, aes(x = factor(year), y = rate, fill = factor(colour))) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(y = rate, label = paste0(round(rate,2), "%")), vjust = -0.7) +
coord_cartesian(clip = 'off') +
ggtitle("% population belonging to 'No religion', England and Wales census") +
theme_minimal() +
xlab("") + ylab("") +
theme(legend.position = "none",
plot.title = element_text(size = 18, face = "bold"),
axis.text = element_text(size = 14))
p
And you can combine all theme element in one theme() function (as did I). The output is:
And you can easily animate the plot using the following code:
library(gganimate)
p + transition_reveal(index)
And the output is as below:
Hope it helps.
So it was answered here although I don't know why the fix works.
For some reason, labels need to go into gganimate as factors
as.factor()
I just had to add the line:
df_new$rate_label <- as.factor(df_new$rate_label)
and it works fine.

Question about ggplot2 bar graph transformation method

I wanted the barplot to appear in two forms, so I created repeated data and used it as an input.
So I used the data in the form below.
I put the data in the form above and wrote the following code to use it.
Select <- "Mbp"
if(Select == "Mbp"){
Select <- "Amount of sequence (Mbp)"
} else if (Select == "Gbp"){
Select <- "Amount of sequence (Gbp)"
}
ggplot(G4, aes(x = INDV, y = Bp, fill = Group)) + theme_light() +
geom_bar(stat = 'identity', position = 'dodge', width = 0.6) + coord_flip() +
scale_x_discrete(limits = rev(unname(unlist(RAW_TRIM[1])))) +
scale_fill_discrete(breaks = c("Raw data","Trimmed data"))+
scale_y_continuous(labels = scales::comma, position = "right") +
theme(axis.text = element_text(colour = "black", face = "bold", size = 15)) +
theme(legend.position = "bottom", legend.text = element_text(face = "bold", size = 15),
legend.title = element_blank()) + ggtitle(Select) + xlab("") + ylab("") +
theme(plot.title = element_text(size = 25, face = "bold", hjust = 0.5))
Then I can get a plot like the one below, where I want the red graph to be on top of the green graph.
I also tried changing the order of the data, and several sites such as the Internet and Stack Overflow provided solutions and used them, but not a single solution was able to solve them.
If you know a solution, please let me know how to modify the code or change the data.
thank you.
You seem to be asking more than one question at once here, but the main one is: why do the bars for Raw appear under those for Trimmed? The short answer is: factor levels and the behaviour of coord_flip().
Let's make a toy dataset:
library(tidyverse)
G4 <- data.frame(INDV = c("C_01", "C_01", "C_41", "C_41"),
Group = c("Raw data", "Trimmed data", "Raw data", "Trimmed data"),
Bp = c(200, 100, 500, 400))
A simple dodged bar chart. Note that Raw comes before Trimmed, because R is before T in the alphabet:
G4 %>%
ggplot(aes(INDV, Bp)) +
geom_col(aes(fill = Group),
position = "dodge")
Now we coord_flip:
G4 %>%
ggplot(aes(INDV, Bp)) +
geom_col(aes(fill = Group),
position = "dodge") +
coord_flip()
This has the effect of reversing the variables, so Raw is now below Trimmed.
We can fix that by altering factor levels. As there are only two groups we can just reverse them using fct_rev() from the forcats package:
G4 %>%
ggplot(aes(INDV, Bp)) +
geom_col(aes(fill = fct_rev(Group)),
position = "dodge") +
coord_flip()
The bar for Raw is now on top but unfortunately, the colours are now reversed so that Raw bars are green. We can fix that using scale_fill_manual():
G4 %>%
ggplot(aes(INDV, Bp)) +
geom_col(aes(fill = fct_rev(Group)),
position = "dodge") +
coord_flip() +
scale_fill_manual(values = c("#00BFC4", "#F8766D"))
Now the Raw bars are on top, and they are red.

How to rotate the x axis in ggplot2 in R code when using grouping bar chart

ggplot2: group x axis discrete values into subgroups
I found this figure on the link above is very useful to create a grouping barchart. However, my question is that how could I change the angle of the letter a,b,c,... etc in the x axis? It worked okay when plotting it, but since I used another code to layout the Arabic letter, the sub-x axis did not rotate
My data is as follows:
rr <- df %>% count(college,department)
View(rr)
And I did the following code, it worked for the grouping value, and the sub-x axis labels did rotate. However, Arabic letters did not appear correctly.
zz<-ggplot(rr, aes(college, n, fill=department, label = department)) +
geom_bar(position="dodge", stat="identity") +
geom_text(position = position_dodge(width = 1), aes(x=college, y=0) ) +
theme(axis.title.y = element_text(size = rel(2), angle = 90), legend.position = "none") + ylim(0,140)+
theme(axis.text.x = element_text(angle = 60,size = rel(3), color="black"))
Now, I am using the following code in order to allow Arabic letters appear correctly, it did not rotate the sub-x axis when I override the previous code with the following:
gg<-ggplotly(zz) %>% layout(titlefont=list(size=10), yaxis = list(side="right", gridcolor = toRGB("gray90"),
gridwidth = 2, ticks="", title="عدد المحاضرات لكل قسم", titlefont=list(size=20)),
xaxis = list(ticks="", tickfont=list(size=14), title="الأقسام الأكاديمية", titlefont=list(size=30)),
margin = list(l = 50, r=30, b = 50, t = 80))
It appeared with vertical sub-x axis as follows:
But I am trying to rotate them in the second code, any answers would be really appreciated, thank you.
Without a reproducible example of your dataset, it is hard to be sure but based on the post you are referring, the letter are plot by the geom_text function, so you can pass angle = 60 into geom_text:
zz<-ggplot(rr, aes(college, n, fill=department, label = department)) +
geom_bar(position="dodge", stat="identity") +
geom_text(position = position_dodge(width = 1), aes(x=college, y=0), angle = 60 ) +
theme(axis.title.y = element_text(size = rel(2), angle = 90), legend.position = "none") + ylim(0,140)+
theme(axis.text.x = element_text(angle = 60,size = rel(3), color="black"))
Here, an example based on the post you are referring (ggplot2: group x axis discrete values into subgroups):
dat <- data.frame(value=runif(26)*10,
grouping=c(rep("Group 1",10),
rep("Group 2",10),
rep("Group 3",6)),
letters=LETTERS[1:26])
library(ggplot2)
ggplot(dat, aes(grouping, value, fill=letters, label = letters)) +
geom_bar(position="dodge", stat="identity") +
geom_text(position = position_dodge(width = 1), aes(x=grouping, y=0), angle = 90)

Left-adjust (hjust = 0) vertical x axis labels on facets with free scale

I have decided to rephrase this question. (Editing would have taken more time and in my opinion would also not have helped the OP.)
How can one left-adjust (hjust = 0, i.e., in text direction) over facets, when scale = 'free_x'?
I don't really think that left-adjustment of x-labels is a very necessary thing to do (long labels generally being difficult to read, and right-adjusting probably the better choice) - but I find the problem interesting enough.
I tried with empty padding to the maximum character length, but this doesn't result in the same length for all strings. Also, setting axis.text.x = element.text(margin = margin()) doesn't help. Needless to say, hjust = 0 does not help, because it is adjusting within each facet.
library(ggplot2)
diamonds$cut_label <- paste("Super Dee-Duper", as.character(diamonds$cut))
ggplot(data = diamonds, aes(cut_label, carat)) +
facet_grid(~ cut, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90))
The red arrows and dashed line indicate how the labels should adjust. hjust = 0 or margins or empty padding do not result in adjustment of those labels over all facets.
Data modification from this famous question
I tried with empty padding to the maximum character length, but this
doesn't result in the same length for all strings.
This caught my attention. Actually, it would result in the same length for all strings if you padded the labels with spaces, made them all the same length, and ensured the font family was non-proportionally spaced.
First, pad the labels with spaces such that all labels have the same length. I'm going to ustilise the str_pad function from the stringr package.
library(ggplot2)
data("diamonds")
diamonds$cut_label <- paste("Super Dee-Duper", as.character(diamonds$cut))
library(stringr)
diamonds$cut_label <- str_pad(diamonds$cut_label, side="right",
width=max(nchar(diamonds$cut_label)), pad=" ")
Then, you may need to load a non-proportionally-spaced font using the extrafont package.
library(extrafont)
font_import(pattern='consola') # Or any other of your choice.
Then, run the ggplot command and specify a proportionally spaced font using the family argument.
ggplot(data = diamonds, aes(cut_label, carat)) +
facet_grid(~cut, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90, family="Consolas"))
One way, and possibly the most straight forward hack, would be to annotate outside the coordinates.
Disadvantage is that the parameters would need manual adjustments (y coordinate, and plot margin), and I don't see how to automate this.
library(ggplot2)
diamonds$cut_label <- paste("Super Dee-Duper", as.character(diamonds$cut))
ann_x <- data.frame(x = unique(diamonds$cut_label), y = -16, cut = unique(diamonds$cut))
ggplot(data = diamonds, aes(cut_label, carat)) +
facet_grid(~cut, scales = "free_x") +
geom_text(data = ann_x, aes(x, y, label = x), angle = 90, hjust = 0) +
theme(
axis.text.x = element_blank(),
plot.margin = margin(t = 0.1, r = 0.1, b = 2.2, l = 0.1, unit = "in")
) +
coord_cartesian(ylim = c(0, 14), clip = "off")
Created on 2020-03-14 by the reprex package (v0.3.0)
I'd approach this by making 2 plots, one of the plot area and one of the axis labels, then stick them together with a package like cowplot. You can use some theme settings to disguise the fact that the axis labels are actually made by a geom_text.
The first plot is fairly straightforward. For the second which becomes the axis labels, use dummy data with the same variables and adjust spacing how you want via text size and scale expansion. You'll probably also want to mess with the rel_heights argument in plot_grid to change the ratio of the two charts' heights.
library(ggplot2)
library(cowplot)
p1 <- ggplot(diamonds, aes(x = cut_label, y = carat)) +
facet_grid(cols = vars(cut), scales = "free_x") +
theme(axis.text.x = element_blank()) +
labs(x = NULL)
axis <- ggplot(dplyr::distinct(diamonds, cut_label, cut), aes(x = cut_label, y = 1)) +
geom_text(aes(label = cut_label), angle = 90, hjust = 0, size = 3.5) +
facet_grid(cols = vars(cut), scales = "free_x") +
scale_x_discrete(breaks = NULL) +
scale_y_continuous(expand = expansion(add = c(0.1, 1)), breaks = NULL) +
labs(y = NULL) +
theme(strip.text = element_blank(),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank())
plot_grid(p1, axis, ncol = 1, axis = "lr", align = "v")
We can edit the text grobs after generating the plot, using library(grid).
g <- ggplot(data = diamonds, aes(cut_label, carat)) +
facet_grid(~cut, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
gt <- cowplot::as_gtable(g)
axis_grobs <- which(grepl("axis-b", gt$layout$name))
labs <- levels(factor(diamonds$cut_label))[order(levels(diamonds$cut))]
for (i in seq_along(axis_grobs)) {
gt$grobs[axis_grobs[i]][[1]] <-
textGrob(labs[i], y = unit(0, "npc"), just = "left", rot = 90, gp = gpar(fontsize = 9))
}
grid.draw(gt)

Space specific ares in x axis ggplot

I have the below script:
testFigure <- ggplot(data = final_df, aes(x=final_df$`ng DNA`,
y=final_df$`count`)) +
geom_point(col = "darkmagenta") + ggtitle("ng VS Number") +
xlab(expression(paste("ng"))) + ylab("Num (#)") +
theme(plot.title = element_text(hjust = 0.5, color="orange", size=18,
face="bold.italic"),
axis.title.x = element_text(color="#993333", size=10, face = "bold"),
axis.title.y = element_text(color="#993333", size=10,face = "bold")) +
scale_y_log10(breaks=c(0,10,50,200,600))
testFigure+scale_x_continuous(breaks=c(5,50,100,150,200,250,300,350,400)
Which generates the plot:
I'd like to space the area of the plot in the X axis, so that the lower values 0-10, but especially 0-5, will be more clear and spaced between them, while keeping the spaces of the other ticks.
Any suggestions how to do that?
Solution 1:
I also noticed you have done it but in wrong axis
scale_y_log10(breaks=c(0,10,50,200,600))
but it seems that you have to do the same thing for x axe as well
scale_x_log10(breaks=c(0,10,50,200,600))
Solution 2:
scale_x_discrete(limits=0:5)
You can use scale on each axis and set a limit over it
library(ggplot2)
dt<-data.frame("Name"=sample(c("A","B"),10,replace = T),
x=sample(1:10,10),y=sample(1:10,10))
ggplot(dt, aes( x= x , y= y))+
geom_point(stat='identity', aes(shape=Name,colour = Name))+
scale_x_discrete(limits=1:12)+
scale_y_discrete(limits=1:12)

Resources