Does anyone have a solution to this graph? The labels above the bar graph are cutoff either at the top or at the bottom, depending on how I do the vertical adjustment. I don't think I can reduce the size of the font enough to fit and be readable.
On the one hand, I have this, where the label is cut off in the first bar for Paid Search
ggplot(all_rb6, aes(x = `Number of Touchpoints`, y = Percent, fill = Campaign)) +
geom_col(position = 'dodge') +
facet_wrap(~Campaign) +
labs(title="Title", subtitle = "subtitle") +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
labs(caption = "Marketing") +
geom_text(aes(label = paste(format(Percent), "%")), vjust = -0.5, position = position_dodge(.9), colour = "black") +
scale_fill_brewer(palette = "Pastel1")
On the other hand, I have this graph, where the labels are cut off for the third bar in all of them.
ggplot(all_rb6, aes(x = `Number of Touchpoints`, y = Percent, fill = Campaign)) +
geom_col(position = 'dodge') +
facet_wrap(~Campaign) +
labs(title="Title", subtitle = "subtitle") +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
labs(caption = "Marketing") +
geom_text(aes(label = paste(format(Percent), "%")), vjust = 1.5, position = position_dodge(.9), colour = "black") +
scale_fill_brewer(palette = "Pastel1")
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 use the following code to generate a series of plots which include both geom_boxplot and geom_point. I want the points to have more distinctive colors without me defining what they are every time:
for (i in 1:length(Girder.Plot)) {
Plot.Girder <- ggplot(data = subset(Girder.Plot[[i]], Names == "Sample"),
aes(x = Type, y = Moment, fill = factor(Spacing,levels = c("9","12","15")))) +
geom_boxplot(outlier.shape = NA, position = position_dodge(width = 0.75)) +
stat_summary(fun = mean, geom="point", shape=23, size=2,
position = position_dodge(width = 0.75)) +
stat_boxplot(geom='errorbar', linetype=1, width=0.5,
position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "No Factor"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 1"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 2"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
geom_point(data = subset(Girder.Plot[[i]], Names == "Factor 3"),
aes(colour = Names), position = position_dodge(width = 0.75)) +
labs(x = element_blank(), y = element_blank(),
title = paste0("Moment Live Load Distribution Factors \n Along the Roadway Width for \n Ultra-Girder Section: UG-"
,str_extract(names(Girder.Plot)[i],"\\d+")),
fill = "Girder Spacing (ft):", colour = element_blank()) +
theme_classic() + ylim(0.4, 1.1) +
theme(plot.title = element_text(hjust = 0.5, margin = margin(45,0,20,0),
face = "bold", size = 18),
legend.title.align = 0.5, legend.position = "bottom",
legend.box.background = element_rect(colour = "black", size = 0.5),
legend.box.margin = margin(0,0,0,0))
print(Plot.Girder)
}
Use scale_fill_brewer/scale_color_brewer.
library(ggplot2)
ggplot(iris, aes(Species, Sepal.Length, fill = Species)) +
geom_boxplot() +
theme_minimal() +
scale_fill_brewer(palette = "Set1")
To see available palettes.
RColorBrewer::display.brewer.all()
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 adjust make the width of the gray area and the bar plot smaller.
Now the grey area on the left and right to the plot is too much as you can see in my screenshot. Looking for a solution that also captures the scenario that I have 3 or more bars in the chart.
ggplot(data=MyDataFrame, aes(x='Text', y=MetricColumn, fill=LegendTag))+
scale_fill_brewer(palette="Set3")+
geom_bar(stat="identity", width=0.2)+
geom_text(aes(label=paste0(MetricColumn,"%")), position = position_stack(vjust = .5), size = 3.5, color = "black")+
labs(title="Text of the title")+
theme(plot.title = element_text(hjust = 0.5, face="bold"))+
theme(legend.position="left")
What James Martherus says is definitly part of the solution. But since a plot always fills out the whole plotting window it will look weird. So youc an either set the plot margins:
ggplot(data=diamonds, aes(x= "Text", y= ..count../sum(..count..), fill = cut,
label = scales::percent(..count../sum(..count..)))) +
scale_fill_brewer(palette="Set3")+
geom_bar(stat="count", show.legend = F) +
geom_text(stat = 'count', position = position_stack(vjust = .5), size = 3, color = "black") +
labs(x = NULL) +
scale_y_continuous(labels = scales::percent, name = "Percent") +
theme(plot.margin = unit(c(0.5,7, 0.5, 7), "cm"))
Or you just save it the way you want it to be:
p <- ggplot(data=diamonds, aes(x= "Text", y= ..count../sum(..count..), fill = cut,
label = scales::percent(..count../sum(..count..)))) +
scale_fill_brewer(palette="Set3")+
geom_bar(stat="count", show.legend = F) +
geom_text(stat = 'count', position = position_stack(vjust = .5), size = 6, color = "black") +
labs(x = NULL) +
scale_y_continuous(labels = scales::percent, name = "Percent") +
theme(axis.title.y = element_text(size = 20),
axis.text = element_text(size = 15))
ggsave("D:/R/plot.png", width = 5, height = 15, dpi = 200)
This way you get it without the margins.
Remove the width=.2 from geom_bar as you're telling R to make your bar take up only 20% of the plot. geom_bar sets the default width at 90% of the plot (see the documentation).
ggplot(data=MyDataFrame, aes(x='Text', y=MetricColumn, fill=LegendTag))+
scale_fill_brewer(palette="Set3")+
geom_bar(stat="identity")+
geom_text(aes(label=paste0(MetricColumn,"%")), position = position_stack(vjust = .5), size = 3.5, color = "black")+
labs(title="Text of the title")+
theme(plot.title = element_text(hjust = 0.5, face="bold"))+
theme(legend.position="left")
I have data that looks like this, df_Filtered:
Product Relative_Value
Car 0.12651458
Plane 0.08888552
Tank 0.03546231
Bike 0.06711630
Train 0.06382191
I want to make a bar plot of the data in GGplot2:
ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
scale_y_continuous(labels = scales::percent) +
geom_bar(stat = "identity") +
theme_bw() +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "none") +
theme(plot.title = element_text(hjust = 0.5))
labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
theme(panel.grid.major = element_blank())
How do i get rid of the decimals on the y-axis in the chart? So that it says 20 % instead of 20.0 %?
Use percent_format from the scales package to set accuracy to 1.
library(ggplot2)
library(scales)
ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
scale_y_continuous(labels = percent_format(accuracy = 1)) +
geom_bar(stat = "identity") +
theme_bw() +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "none") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
theme(panel.grid.major = element_blank())
DATA
df_Filtered <- read.table(text = "Product Relative_Value
Car 0.12651458
Plane 0.08888552
Tank 0.03546231
Bike 0.06711630
Train 0.06382191",
header = TRUE, stringsAsFactors = FALSE)
scales::percent_format(accuracy = 2) doesn't allow manual breaks = c(0, 0.5, .10).
So, I have to create the manual function scale_y_continuous(breaks = c(0, 0.5, .10), labels = function(x) paste0(round(as.numeric(x*100)), "%")) .