Related
I have color for each variable (fishing strategy), however, if I put it in a facet grid like this, based on the years, I can't set up the colors accordingly. I want to have one color for each fishing strategy instead of one color for each year, but also need the legend for fishing strategies with color or just years without color. But I didn't manage to do that. Can someone help me with this?
With this code:
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year))) + geom_bar(stat="identity", position="dodge")
+ theme_minimal()
spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free")
+ labs(fill = "Year", x = "Fishing strategies", y = "Total REA", title = "Based on the REA")
+ theme(text = element_text(size = 13))
+ theme(legend.position = "bottom")
+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+
scale_fill_manual(values = c("GIL_COD" = "#004c6d",
"GIL_FRS" = "#00ffff",
"GIL_FLE" = "#00a1c1",
"GIL_HER" = "#00cfe3",
"PAS_FLA" = "#78ab63",
"POL_FRS" = "#6efa75",
"BST_MIX" = "#ffc334",
"MPT_HER" = "#ff9509",
"BPT_HER" = "#ffb6de",
"BPT_COD" = "#cc0089"))
I get this
but if I removed the scale fill manual part, it looked like this
I think I've worked it out, but I don't have your data. (It's a bit messy.)
I used the dataset diamonds and renamed the fields. The first plot is supposed to represent your second plot, where you have removed the scale_color_manual.
The data first:
library(tidyverse)
# create variables
io1 <- diamonds %>%
mutate(year = cut,
effort = x,
clu_name2 = color,
vessel_category = rep(c("Passive","Active"), nrow(diamonds)/2),
geartype_clu2 = rep(LETTERS[1:3], nrow(diamonds)/3))
levels(io1$year) <- c(2019:2023)
Original plot as you've coded:
# grid faceting/color
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year))) +
geom_col(position = "dodge") +
theme_minimal()
spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "Year", x = "Fishing strategies", y = "Total REA",
title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
The primary differences are the arguments group = year and fill = clu_name2.
p2 <- ggplot(io1, aes(x = clu_name2, y = effort, group = year, fill = clu_name2)) +
geom_col(position = "dodge") +
theme_minimal()
p2 + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "", x = "Fishing strategies\ngrouped by years",
y = "Total REA", title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Keep in mind the legend here is fishing strategies. If you want the years shown, perhaps a second depth label with scale_fill_manual() would be a good approach.
Now as far getting the years and the strategies in the legend or as legends. You may be better off with adding a second scale axis and using the first version. This can be done with the package ggnewscale. You'll have to adjust the font size or expand or add to the margin, though.
# grid faceting/color
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year),
group = year)) +
geom_col(position = "dodge") +
theme_minimal()
spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "Year", x = "Fishing strategies", y = "Total REA",
title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
ggnewscale::new_scale("fill") + # added scale here
geom_tile(aes(fill = clu_name2, y = 1)) +
scale_fill_discrete(name = "Strategies")
It doesn't quite work out when using it with the second option
p2 <- ggplot(io1, aes(x = clu_name2, y = effort, group = year, fill = clu_name2)) +
geom_col(position = "dodge") +
theme_minimal()
p2 + facet_grid(vessel_category~geartype_clu2, scales = "free") +
labs(fill = "", x = "Fishing strategies\ngrouped by years",
y = "Total REA", title = "Based on the REA") +
theme(text = element_text(size = 13)) +
theme(legend.position = "bottom") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
scale_fill_manual(values = c("D" = "#004c6d",
"E" = "#00ffff",
"F" = "#00a1c1",
"G" = "#00cfe3",
"H" = "#78ab63",
"I" = "#6efa75",
"J" = "#ffc334",
"K" = "#ff9509",
"L" = "#ffb6de",
"M" = "#cc0089")) +
ggnewscale::new_scale("fill") +
geom_tile(aes(fill = year, y = 1)) +
scale_fill_viridis_d(name = "Years")
I build this graph:
labels.minor <- c("nie","selten","manchmal", "mehrmals", "oft", "sehr oft", "immerzu")
df_ebf <- df_ebf %>%
map_df(rev)
ggplot(data=df_ebf, aes(x=forcats::fct_inorder(Skalen), y=Werte, group="")) +
geom_line(aes(y = Werte, color = "#003560")) +
geom_line(aes(y = SD_plus, color = "#8DAE10", linetype = "dashed")) +
geom_line(aes(y = SD_minus, color = "#8DAE10",linetype = "dashed")) +
geom_point(color = "#003560") +
coord_flip() +
labs(x="EBF-Skalen") +
scale_y_continuous(limits = c(0, 6), breaks = c(0,1,2,3,4,5,6), labels = paste0(0:6, "\n", labels.minor), sec.axis = sec_axis(~.x, breaks = 0:6)) +
scale_x_discrete(expand = c(0,0)) +
theme(panel.grid.major.y = element_blank(),panel.grid.minor.x = element_blank(),axis.line.x = element_line(size = 1, colour = "black", linetype=1),axis.title=element_blank())
But instead of changing the style of the lines, the styling just appears in the legend.
take them out of the aes:
aes(...), color="..", linetype=".."
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 want to create a plot with a single vertical bar (colored continuously), with a mark on it showing the score for a particular person. Image:
I can generate the colored bar in ggplot, but only as a legend (not the actual plot). For example the legend resulting from the following is fine:
ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) +
geom_point() +
scale_color_gradientn(colors = rainbow(5))
Is there any way to do this? Any help would be really appreciated - I'm completely stuck on this.
ggplot(data.frame(y = 51), aes( y=y)) +
geom_tile(data = data.frame(y = 0:100),
aes(x= 0.5, y = y, fill = y)) +
geom_segment(aes(x=0, xend=1, yend=y)) +
geom_text(aes(label = y, x = 1), hjust = -0.3) +
coord_cartesian(clip = "off", xlim = c(0,1.2)) +
scale_fill_gradientn(colors = rainbow(5)) +
scale_x_continuous(labels = NULL) +
guides(fill = FALSE) +
theme_minimal() +
theme(line = element_blank()) +
labs(x="", y = "")
I have the following ggplot2 chart. I don't want transparency on the value labels.
Code:
ggplot(test, aes(x = reorder(org, -as.numeric(level)), y = obsAvg, fill = level, alpha = round)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) +
scale_alpha_manual(values = c(.5, .75, 1), guide = FALSE) +
labs(title = "Average Observation Score by Round", y = "", x = "", fill = "Group") +
theme_bw() +
geom_text(aes(label = round(obsAvg,1)), vjust = -.5, size = 4, fontface="bold", position = position_dodge(width = .9)) +
scale_y_continuous(limits = c(0,4), expand = c(0,0)) +
theme(legend.position="bottom")
Data:
set.seed(1)
test <- data.frame(
org = rep(c("Mammals", "Cats", "Tigers", "Lions", "Cheetahs"), 3),
level = rep(c("Animals", "Family", rep("Species", 3)), 3),
group = rep("Cats",15),
round = rep(c("Round1", "Round2", "Round3"),5),
obsAvg = runif(15, 1, 4)
)
I have tried moving the alpha = round to be an aesthetic of geom_bar() but then I lose the dodge of the labels:
How can I replicate the top chart but not apply the transparency aesthetic to my labels?
I would move the aes(alpha=) to geom_bar and then add an aes(group=) to geom_text to recover the dodging.
ggplot(test, aes(x = reorder(org, -as.numeric(level)), y = obsAvg, fill = level)) +
geom_bar(aes(alpha=round), stat = "identity", position = "dodge") +
scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) +
scale_alpha_manual(values = c(.5, .75, 1), guide = FALSE) +
labs(title = "Average Observation Score by Round", y = "", x = "", fill = "Group") +
theme_bw() +
geom_text(aes(label = round(obsAvg,1), group=round), vjust = -.5, size = 4, fontface="bold", position = position_dodge(width = .9)) +
scale_y_continuous(limits = c(0,4), expand = c(0,0)) +
theme(legend.position="bottom")
That's a pretty plot.