I want to change the text size of my y axis descrption and center my plottitle.
Everything coded within the themes function is not being applied to my graph.
Where is the problem?
finalchart = ggplot(euall,
aes(day, cumulative_cases_of_14_days_per_100000 ,
group = countriesAndTerritories)) +
geom_bar(stat = "identity" ,
col = "white" ,
fill = "darkred") +
facet_wrap("countriesAndTerritories") +
geom_line(aes(y = rollmean(cumulative_cases_of_14_days_per_100000, 1,
na.pad = TRUE),
col = "pink"),
show.legend = FALSE) +
labs(title = "COVID infections in the European Union in September 2020" ,
x = "\nSeptember" ,
y = "Infections per 100'000\n" ,
caption = "source: https://opendata.ecdc.europa.eu/covid19/casedistribution/csv") +
theme(axis.text.y = element_text(size = 5) ,
axis.title.y = element_text(size = 10) ,
plot.title = element_text(hjust = 0.5)) +
theme_minimal()
finalchart
The problem is the order in which you call theme() and theme_minimal(). By calling theme_minimal() second your manual settings in theme() are overwritten.
library(ggplot2)
library(patchwork)
p1 <- ggplot(data = cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("theme_minimal second") +
theme(title = element_text(size = 24, color = "red", face = "bold")) +
theme_minimal()
p2 <- ggplot(data = cars, aes(x = speed, y = dist)) +
geom_point() +
ggtitle("theme_minimal first") +
theme_minimal() +
theme(title = element_text(size = 24, color = "red", face = "bold"))
p1+p2
Related
I am trying to generate a bar graph in R, but as the y-axis values are very close, there is no difference in the graph.
Can someone help me?
This is the data I use to generate the chart.
rede <- c("Wifi", "Wifi(AB)", "Wifi(AB) + 4G(AB)", "Wifi(AB) + 4G(B)", "4G(AB)")
disp <- c(0.9981663483026838, 0.9979983253954591, 0.9983305230561498, 0.9981898613052699, 0.9980460877265795)
down <- c(16.062788868489800, 17.534669535778500, 14.624618028127900, 15.85681496583588, 17.116271515163100)
dados <- data.frame("Ref" = rede, "Disponibilidade" = disp, "Downtime" = down)
ggplot(dados) +
aes(
x = Ref,
fill = Disponibilidade,
weight = Disponibilidade
) +
geom_bar(position = "fill") +
scale_fill_viridis_c(option = "plasma", direction = 1) +
labs(
x = "Redes",
y = "Valores",
title = "Gráfico Disponibilidade"
) +
theme_minimal() +
theme(
plot.title = element_text(size = 18L,
face = "bold",
hjust = 0.5)
)
As others mentioned in the comments, it's unclear what exactly you are looking for, but you could do something like this (as #JonathanV.Solórzano recommended with using coord_cartesian).
library(ggplot2)
ggplot(dados) +
aes(
x = Ref,
y = Disponibilidade,
fill = Disponibilidade,
weight = Disponibilidade
) +
geom_col() +
scale_fill_viridis_c(option = "plasma", direction = 1) +
coord_cartesian(ylim = c(min(dados$Disponibilidade), max(dados$Disponibilidade))) +
labs(x = "Redes",
y = "Valores",
title = "Gráfico Disponibilidade") +
theme_minimal() +
theme(plot.title = element_text(size = 18L,
face = "bold",
hjust = 0.5))
Output
Alternative with geom_bar (which produces the same output as above)
ggplot(dados) +
aes(
x = Ref,
fill = Disponibilidade,
weight = Disponibilidade
) +
geom_bar() +
scale_fill_viridis_c(option = "plasma", direction = 1) +
coord_cartesian(ylim = c(min(dados$Disponibilidade), max(dados$Disponibilidade))) +
labs(x = "Redes",
y = "Valores",
title = "Gráfico Disponibilidade") +
theme_minimal() +
theme(plot.title = element_text(size = 18L,
face = "bold",
hjust = 0.5))
I have the dataframe below
GO<-c("cytosol (GO:0005829)","cytosol (GO:0005829)")
FE<-c(2.70,4.38)
FDR<-c(0.00159,0.00857)
Facet<-c("ileum 24h","ileum 72h")
CCC<-data.frame(GO,FE,FDR,Facet)
and with this code
CCC %>%
arrange(desc(CCC$GO))%>%
ggplot(aes(x = FDR, y = GO, size = FE, color = FDR)) +
geom_point(alpha = 0.5) +
scale_size(range = c(5, 8), name = "Fold enrichment") +
facet_grid(cols = vars(Facet), scales = "free") +
theme(axis.title.x=element_blank(),axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1)) +
scale_y_discrete(name = "GO biological process complete") +
scale_x_continuous(name = "FDR") +
scale_colour_gradient(low = "yellow", high = "red", name = "FDR") +
theme_bw()
I create a bubble plot with facets. But I want to delete the x-axis title 'FDR' and display the labels with an angle but despite setting the theme() it does not change.
You have put theme_bw() at the end, which over-writes your theme call. Put your custom themes at the end:
CCC %>%
arrange(desc(CCC$GO))%>%
ggplot(aes(x = FDR, y = GO, size = FE, color = FDR)) +
geom_point(alpha = 0.5) +
scale_size(range = c(5, 8), name = "Fold enrichment") +
scale_y_discrete(name = "GO biological process complete") +
scale_x_continuous(name = "FDR") +
scale_colour_gradient(low = "yellow", high = "red", name = "FDR") +
facet_grid(cols = vars(Facet), scales = "free") +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1))
I think it is good practice to lay out your calls to ggplot in a consistent way so that this kind of thing doesn't happen:
Call ggplot +
Geom (and stat) layers, ordered depending on which ones you want on top +
Scales +
Facets +
Labels and titles +
Global themes like theme_bw() +
Individual theme tweaks via theme
Only change the position of theme_bw():
library(tidyverse)
#Data
GO<-c("cytosol (GO:0005829)","cytosol (GO:0005829)")
FE<-c(2.70,4.38)
FDR<-c(0.00159,0.00857)
Facet<-c("ileum 24h","ileum 72h")
CCC<-data.frame(GO,FE,FDR,Facet)
#Plot
CCC %>%
arrange(desc(CCC$GO))%>%
ggplot(aes(x = FDR, y = GO, size = FE, color = FDR)) +
geom_point(alpha = 0.5) +
scale_size(range = c(5, 8), name = "Fold enrichment") +
facet_grid(cols = vars(Facet), scales = "free") +
xlab('')+
theme_bw()+
theme(axis.title.x=element_blank(),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1)) +
scale_y_discrete(name = "GO biological process complete") +
scale_x_continuous(name = "") +
scale_colour_gradient(low = "yellow", high = "red", name = "FDR")
Output:
I have the following example plot:
test <- data.frame("Factor" = as.factor(c("O", "C", "A")),
b = c(0.18, .34, .65, -.13, .38, .26),
lower95 = c(-.1, .09, .34, -.52, .10, -.02),
upper95 = c(.48, .58, .98, .26, .67, .56),
group = factor(c("Experiment 1","Experiment 2")))
test$Factor <- factor(test$Factor, as.character(test$Factor))
test$group <- factor(test$group, as.character(test$group))
ggplot(test, aes(Factor, b, colour = group)) +
geom_errorbar(aes(ymin = lower95, ymax = upper95),
size = 1,
width = .5,
position = 'dodge') +
geom_hline(yintercept = 0) +
ylim(-1.25, 1.25) +
coord_flip() +
theme_bw() +
ggtitle("Title") +
theme(
axis.text=element_text(size = 20),
axis.title=element_text(size = 18),
plot.title = element_text(size = 20, face = "bold"),
axis.text.y=element_text(size = 12)
)
As you'll see, the error bars appear in the reverse order (from top to bottom) as they do in the legend. I would like Experiment 1 error bars to appear above Experiment 2 error bars.
I have tried
ggplot(test, aes(Factor, b, colour = forcats::fct_rev(groups)
But this reverses the order of the group labels in the legend, not the order of the colours in the legend–which is what would work. I have also tried reversing the order in which I enter them in the data frame and this does not solve the problem.
I would appreciate some help!
Re-factoring will change the order of the plot, but, as you saw, also changes the order of the legend. In addition to reversing the levels of group, you can reverse the order the legend is displayed with the reverse argument in guide_legend.
ggplot(test, aes(Factor, b, colour = forcats::fct_rev(group))) +
geom_errorbar(aes(ymin = lower95, ymax = upper95),
size = 1,
width = .5,
position = 'dodge') +
geom_hline(yintercept = 0) +
ylim(-1.25, 1.25) +
coord_flip() +
theme_bw() +
ggtitle("Title") +
theme(
axis.text=element_text(size = 20),
axis.title=element_text(size = 18),
plot.title = element_text(size = 20, face = "bold"),
axis.text.y=element_text(size = 12)
) +
guides(color = guide_legend(reverse = TRUE) )
If you are using scale_color_discrete or scale_color_manual to control other scale elements like the legend name, you can use guide_legend there instead of via guides.
+
scale_color_discrete(name = "Experiment", guide = guide_legend(reverse = TRUE) )
Do you mean something like this?
test$Factor <- factor(test$Factor, levels = rev(levels(test$Factor)));
test$group <- factor(test$group, levels = rev(levels(test$group)));
ggplot(test, aes(Factor, b, colour = group)) +
geom_errorbar(aes(ymin = lower95, ymax = upper95),
size = 1,
width = .5,
position = 'dodge') +
geom_hline(yintercept = 0) +
ylim(-1.25, 1.25) +
coord_flip() +
theme_bw() +
ggtitle("Title") +
theme(
axis.text=element_text(size = 20),
axis.title=element_text(size = 18),
plot.title = element_text(size = 20, face = "bold"),
axis.text.y=element_text(size = 12)
)
I'm not entirely clear on whether you want to reverse the ordering of test$Factor as well; just (un)comment the corresponding line depending on what you're after.
I've created a heatmap on temperatures by city. ggplot orders cities by default, from Z-A, not what I want. How can I change the code so that the cities would be ordered A-Z, how overall x and y can be ordered in ggplot ?
ggplot(Cities, aes(x = Month, y = City, fill = AvgTemp, frame = City)) +
geom_tile(color = "white", size = 0.5) +
scale_fill_gradient(name = "Average Temperature",low = "blue", high = "red") +
coord_equal() +
labs(x = "Month", y = "", title = "Average Temp") +
theme_tufte() +
theme(axis.ticks = element_blank()) +
theme(axis.text = element_text(size = 15)) +
theme(plot.title = element_text(size = 15)) +
theme(legend.title = element_text(size = 10)) +
theme(legend.text = element_text(size = 10))
I've included a reorder in your aes(y=).
ggplot(data=Cities) +
geom_tile( aes(x=Month, y=reorder(City, AvgTemp, median, order=TRUE), fill = AvgTemp), color = "white", size = 0.5) +
scale_fill_gradient(name = "Average Temperature",low = "blue", high = "red") +
coord_equal() +
labs(x = "Month", y = "", title = "Average Temp") +
theme_tufte()
I made horizontal barplot. I need to move x-axis up, so it is placed not under the last bar, but under some bar, picked based on other criterion.
I've tried some things, like gtable, but with no success. I would appreciate any help.
This is a picture that illustrats what I want to achieve:
Here is the code to produce sample horizontal barplot:
library("ggplot2")
library("RColorBrewer")
colours <- brewer.pal(11, "RdYlGn")[3:9]
no.names <- 4
name.percentage <- data.frame(name = paste0(LETTERS[1:no.names], letters[1:no.names], sample(LETTERS[1:no.names], size = no.names, replace = TRUE )), percentage = 0.85 + runif(no.names, 0, 0.15))
name.percentage <- rbind(
transform(name.percentage, type = 1, fill = cut(percentage, breaks = c(-Inf,(1:6 * 3 + 81)/100, Inf), right = T, labels = colours)),
transform(name.percentage, percentage = 1 - percentage, type = 2, fill = "#EEEEEE")
)
plot <- ggplot(data = name.percentage,
aes( x = name, y = percentage, fill = fill)) +
geom_bar(stat = "identity", position = "stack", width = 0.75) +
scale_fill_identity(guide = "none") +
labs(x = NULL, y = NULL) +
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0)) +
coord_flip() +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_text(size = 11, colour = "black" ),
axis.text.x = element_text(size = 11, colour = "black" ),
axis.line = element_blank(),
plot.margin = grid::unit(c(5,5,5,5),"mm"),
aspect.ratio = ((no.names %% 30) / 30 ) * 1.70)
print(plot)
You could create two separate plots first, removing the axis ticks and labels in one of them entirely:
plot1 <- ggplot(data = subset(name.percentage, name=="AaC" | name=="BbA"),
aes( x = name, y = percentage, fill = fill)) +
geom_bar(stat = "identity", position = "stack", width = 0.75) +
scale_fill_identity(guide = "none") +
labs(x = NULL, y = NULL) +
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0)) +
coord_flip() +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_text(size = 11, colour = "black" ),
axis.text.x = element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
aspect.ratio = ((no.names %% 30) / 30 ) * 1.70)
plot2 <- ggplot(data = subset(name.percentage, name=="CcA" | name=="DdD"),
aes( x = name, y = percentage, fill = fill)) +
geom_bar(stat = "identity", position = "stack", width = 0.75) +
scale_fill_identity(guide = "none") +
labs(x = NULL, y = NULL) +
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0)) +
coord_flip() +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_text(size = 11, colour = "black" ),
axis.text.x = element_text(size = 11, colour = "black" ),
axis.line = element_blank(),
aspect.ratio = ((no.names %% 30) / 30 ) * 1.70)
Then you can use plot_grid from package cowplot to arrange the two plots, with align="h" aligning both plots horizontally:
library(cowplot)
plot_grid(plot2, plot1, align="h", ncol=1)