Labelling and theme of ggalluvial plot in R - r

For the past hours I have tried to understand the whole design/theme/labelling concept around ggalluvial, but I failed.
Within minutes I was able to produce the kind of graph I want (thanks to the package ggalluvial), but I can't figure how to produce the correct labelling/theme I'd like to have.
This is where I am currently:
This is where I was initially:
To get you to understand where I am, here's a reproducible example
I used the following code:
library(ggalluvial)
ds <- as.data.frame(Titanic)
ggplot(ds,
aes(weight = Freq, axis1 = Sex, axis2 = Class)) +
geom_alluvium(aes(fill = Sex), width = 1/12) +
geom_stratum(width = 1/4, fill = "black", color = "grey") +
scale_x_continuous(breaks = 1:2, labels = c("Sex", "Class")) +
scale_fill_manual(name = "", values=c("#A0A0A0", "#494949")) +
ggtitle("Titanic Survival") +
theme_bw() +
geom_text(stat = "stratum", color="white",label.strata = TRUE,
angle=c(90,90,0,0,0,0) , size=6,
nudge_y=c(1,2,3,4,5,0)) +
theme(legend.position = "bottom",
title = element_text(size = 20),
legend.text = element_text(size = 20),
axis.text.y = element_blank(),
axis.text.x = element_text(size=20))
What I would like to change:
turn labels on the left vertically
done
make labels on the right not overlap (not visible with the Titanic data)
I tried nudging. But it may be more useful to place these legends outside the graph, maybe like on an axis?
remove y-axis labels
done
Understand with what command I can change each of the text elements size (title, legend, labels, axis labels)
dome
I am very happy for any help on this. Thank you.

You can try:
ggplot(ds,
aes(weight = Freq, axis1 = Sex, axis2 = Class)) +
geom_alluvium(aes(fill = Sex), width = 1/12) +
geom_stratum(width = 1/16, fill = "black", color = "grey") +
scale_x_continuous(breaks = 1:2, labels = c("Sex", "Class")) +
scale_fill_manual(name = "", values=c("#A0A0A0", "#494949")) +
ggtitle("Titanic Survival") +
theme_bw() +
geom_text(stat = "stratum", color="red",label.strata = TRUE, angle=c(0,0,45,45,45,45),size=5) +
theme(legend.position = "bottom",
title = element_text(size = 20),
legend.text = element_text(size = 20),
axis.text.y = element_blank(),
axis.text.x = element_text(size=20))
add a vector with the same length of labels like angle=c(0,0,45,45,45,45)
add axis.text.y = element_blank() in theme()
pending
Sizes via theme
via theme() such as x-axis labels
text in plot via size=5 in geom_text()

Related

Dodge failing in violin plot

I would like to plot the congruence effects (incongruent minus congruent) as a violin plot per combination of stimulus age and response type. This is what my code looks like so far. I am not yet satisfied with the representation. How can I change it so that for each of the four conditions (adult frown, adult smile, child frown, child smile) I get the corresponding violin plot horizontally next to each other? Thanks in advance for the help. Attached is the code and an excerpt from the data frame.
violin plot
dataset$congruency_effect <- ifelse(dataset$congruency == "congruent", dataset$avgAmplitude, -dataset$avgAmplitude)
p <- ggplot(dataset, aes(x = stimulusResponse, y = congruency_effect, fill = congruency_effect, group = stimulusAge)) +
geom_violin() +
geom_point(position = position_dodge(width = 0.75), size = 3, stat = "summary", fun.y = "mean") +
scale_fill_manual(values = c("#F8766D", "#00BFC4")) +
ggtitle("Conventional EEG 350-450 ms") +
scale_y_continuous(limits = c(-5, 5)) +
facet_wrap(~stimulusAge, scales = "free_x")
EEG_Conventional450_age_response <- p + theme(
# Set the plot title and axis labels to APA style
plot.title = element_text(face = "bold", size = 16),
axis.title = element_text(face = "bold", size = 14),
# Set the axis tick labels to APA style
axis.text = element_text(size = 12),
# Set the legend title and labels to APA style
legend.title = element_text(face = "bold", size = 14),
legend.text = element_text(size = 12),
# Set the plot and panel backgrounds to white
panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill = "white")
)
EEG_Conventional450_age_response
excerpt data frame
several permutations of arguments in ggplot
This has to do with the grouping aesthetic. Remove it, and your plot works.
library(ggplot2)
set.seed(42)
dataset <- data.frame(stimulusResponse = rep(c("frown", "smile"), each = 20),
congruency_effect = rnorm(40),
stimulusAge = rep(c("baby", "adult"), 20))
## removed group = stimulusAge
ggplot(dataset, aes(x = stimulusResponse, y = congruency_effect)) +
geom_violin() +
geom_point(position = position_dodge(width = 0.75), size = 3, stat = "summary") +
facet_wrap(~stimulusAge, scales = "free_x")

In R, ggplot for a population pyramid: how to align labels near to the axis with geom_bar geom_label after flipping the coordinates

I am making a sort of population pyramid using ggplot (plotrix doesn't allow me to do fancy labels etc), then I start with a geom_bar with labels and later I flip the coordinates. Sadly, labels almost cannot being seeing. I would like to move those labels near to the "y- axis" in the middle, that now is showing the age groups.
Data is here: d <- data.frame(age.grp2 = c("1-10", "11-20", "21-30", "31-40", "41-50", "1-10", "11-20", "21-30", "31-40", "41-50"),
sex = c("Female","Female","Female","Female","Female","Male","Male","Male","Male","Male" ),
n.enroll = c(288,500,400,300,200,300,460,300,200,300),
proportion = c(17.1,29.6,23.7,17.8,11.8,51,47.9,42.9,40,60),
proportion2 = c(-17.1,-29.6,-23.7,-17.8,-11.8,51,47.9,42.9,40,60)) My code is this one: ggplot(d, aes(x = age.grp2, y = proportion2, fill = sex)) +
geom_bar(position = position_dodge(width=1), stat='identity') +
geom_label(aes(label = paste(n.enroll," (",proportion,"%)", sep=""), group = factor(sex)),
fill="white", colour = "black",
position= position_dodge(width=1),
size = 3) +
scale_fill_manual(values=c("#BFD5E3", "grey")) +
facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE) +
coord_flip() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
#panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "none",
#axis.line.x = element_line(color = "black"),
axis.ticks.y = element_blank(),
axis.text.x = element_text(colour = "black", size = 8, face = "bold", angle=0, hjust=0.5),
axis.text.y = element_text(colour = "black", size = 8, face = "bold"),
axis.title.x = element_text(size = 14, face="bold", margin = margin(t = 30, r = 20, b = 10, l = 20)),
plot.margin = unit(c(1,1,1,1),"cm")) +
labs(y = "Enrollment percentage within sex",x="") I am attaching also the plot, where we can see in females the label in the age group 11-20 is cut. I would like to have all labels near to the age group labels, within each bar: female labels moved to the right and male labels move it to the left. Also, I would like to have each x-axis extended to 100% or at least in same range, in females goes up to 30% and in males goes up to 60%. Thanks for all the comments
Here's a minimal solution using the base ggplot package, without most of your formatting. The key part is to add a conditional y = ... into the geom_label(aes()) section:
d %>%
mutate(
label = str_c(n.enroll, " (", proportion, "%)"),
label_loc = if_else(sex == "Female", -9.5, 3),
proportion_for_chart = if_else(sex == "Female", -proportion, proportion)
) %>%
ggplot(aes(x = age.grp2, y = proportion_for_chart, fill = sex)) +
geom_col(show.legend = FALSE) +
geom_label(aes(y = label_loc, label = label), size = 3, fill = "white", hjust = 0) +
coord_flip() +
facet_wrap(~ sex, scales = "free") +
theme(
axis.title = element_blank()
)
Whenever possible, I try to reshape data and use geom_col rather than try to get lucky with geom_bar. You should be able to play around with different hard-coded values of y in the geom_label call to fix the proper location for your labels based on your formatting and image size/scale.

add labels to stacked bar chart with proportions using ggplot

I am trying to include labels to a stacked bar chart for proportions. I have been able to create the chart but I haven't been able to successfully add the labels. I've gotten labels but they are centered and overwriting one another....I know this has been asked, but I haven't found a solution that works.
The data is tidy in the sense that it is long
ggplot(data=dat) +
geom_bar(stat="identity",
mapping = aes(x=Vintage, y=OrigAmt, fill=fct_rev(Grade)),
position = "fill") +
ggtitle("Proportions by Grade") +
scale_fill_manual(values = c("grey", "gray40", "black")) +
guides(fill = guide_legend(reverse = TRUE, title="Grade")) +
scale_y_continuous(name="Proportion",label=percent_format()) +
theme_bw() +
theme(plot.title = element_text(hjust=0.5),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_text(angle = 90))
Try adding this after geom_bar
geom_text(aes(y = cumsum(OrigAmt)-OrigAmt/2, label = Grade), show.legend = F)
Changing the y to a cumulative summation (cumsum) of the y variable minus half the y variable places the text in the middle of each section of each bar.
You can also add a color aesthetic and maybe turn Grade into a factor with defined color values
This appears to work.
ggplot(data = dat,
aes(y = freq, x = Vintage, fill = fct_rev(Grade))) +
geom_col() +
geom_text(aes(label = paste0(freq,"%")),
position = position_stack(vjust = 0.5), size = 2) +
scale_y_continuous(
labels = dollar_format(suffix = "%", prefix = "")) +
labs(title = "Distribution by Vintage",
subtitle = "xxx") +
labs(x = NULL, y = "Percentage") +
theme_bw() +
theme(legend.position = "bottom",
legend.direction = "horizontal",
legend.title = element_blank()) +
guides(fill = guide_legend(reverse = T)) +
scale_fill_manual(values = c("grey", "gray40", "brown")) +
theme(axis.text.x = element_text(angle = 90),
axis.text.x.bottom = element_text(vjust = 0.5))
However, I want to change the font colors and I can't see to be able to do that successfully.
For example, I want the label fonts to be BOLD and the font for A and B to be WHITE and the font for C to be BLACK. Thanks!

Can this type of plot be done with ggplot2?

I am using R and ggplot2 to do some plots for publishing purposes. I have come across this plot and I would like to replicate it using ggplot2. However, I have never seen a plot like this made using ggplot2.
Can it be done with ggplot2? What about the text below the bars? I guess these will have to be hard coded in the ggplot2 codes. And how do you align those text?
This gets fairly close:
# Generate sample data (I'm too lazy to type out the full labels)
df <- data.frame(
perc = c(60, 36, 44, 41, 42, 57, 34, 52),
type = rep(c("blue", "green"), 4),
label = rep(c(
"Individual reports created as needed",
"Regular reports on single topics",
"Analytics using data integrated from multiple systems",
"Business unit-specific dashboards and visuals"), each = 2))
library(ggplot2)
ggplot(df, aes(1, perc, fill = type)) +
geom_col(position = "dodge2") +
scale_fill_manual(values = c("turquoise4", "forestgreen"), guide = FALSE) +
facet_wrap(~ label, ncol = 1, strip.position = "bottom") +
geom_text(
aes(y = 1, label = sprintf("%i%%", perc)),
colour = "white",
position = position_dodge(width = .9),
hjust = 0,
fontface = "bold") +
coord_flip(expand = F) +
theme_minimal() +
theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text = element_text(angle = 0, hjust = 0, face = "bold"))
A few explanations:
We use dodged bars and matching dodged labels with position = "dodge2" (note that this requires ggplot_ggplot2_3.0.0, otherwise use position = position_dodge(width = 1.0)) and position = position_dodge(width = 0.9), respectively.
We use facet_wrap and force a one-column layout; strip labels are moved to the bottom.
We rotate the entire plot with coord_flip(expand = F), where expand = F ensures that left aligned (hjust = 0) facet strip texts align with 0.
Finally we tweak the theme to increase the overall aesthetic similarity.
You can try using the data from the other answer. Differences are: we use scales::percent to draw percents. We use ggpubr::theme_transparent() theme to tweak as less as possible.
df$perc <- c(.60, .36, .44, .41, .42, .57, .34, .52)
ggplot(df, aes(label, perc, label=scales::percent(round(perc,2)),fill= factor(type))) +
geom_col(position = position_dodge(0.9), show.legend = F) +
geom_text(aes(y=0), position = position_dodge(0.9), size=5, hjust=-0.1, color="white", fontface="bold") +
scale_y_continuous("",labels = scales::percent) +
coord_flip(expand = F) +
facet_wrap(~label,scales = "free", strip.position = "bottom", ncol = 1) +
ggpubr::theme_transparent() +
xlab("") +
theme(strip.background = element_blank(),
strip.text = element_text(size = 12, face = "bold",hjust=0))
Maybe using facet wrap and adjusting the style?
dat <- data.frame(perc = c(60, 20, 90, 30), col = rep(c("gr1", "gr2"), 2),
text = c(rep("text1", 2), rep("text2", 2)))
ggplot(dat, aes(y = perc, x = col, fill = col)) +
geom_bar(stat = "identity", position = "dodge") +
coord_flip() +
facet_wrap(~text, strip.position = "bottom", ncol = 1)

How to add extra legend to a map plot in ggplot2 based on column entries?

Data: Data
Code:
palette = brewer.pal(11,"RdYlGn") # ColorBrewewr.org spectral palette, 11 colors
ggmap_byscen = ggplot(wmap_byscen.df[wmap_byscen.df$variable !=c("AVG") &
wmap_byscen.df$ID_1 !=c("0"),], aes(x=long, y=lat, group=group))
ggmap_byscen = ggmap_byscen + geom_polygon(aes(fill=value)) + facet_wrap(~ variable)
ggmap_byscen = ggmap_byscen + geom_path(colour="grey50", size=.1)
ggmap_byscen = ggmap_byscen + geom_text(aes(x=c.long, y=c.lat, label=ID_1),size=5)
ggmap_byscen = ggmap_byscen + scale_fill_gradientn(name="% Change",colours=palette)
ggmap_byscen = ggmap_byscen + coord_fixed(xlim = longlimits, ylim = latlimits)
ggmap_byscen = ggmap_byscen + scale_y_continuous(breaks=seq(-60,90,30), labels=c("60ºS","30ºS","0º","30ºN","60ºN","90ºN"))
ggmap_byscen = ggmap_byscen + scale_x_continuous(breaks=seq(-180,180,45), labels=c("180ºW","135ºW","90ºW","45ºW","0º","45ºE","90ºE","135ºE","180ºE"))
ggmap_byscen = ggmap_byscen + labs(x="",y="",title="Average yield impacts across all crops across\nby climate scenarios (% change)")
ggmap_byscen = ggmap_byscen + theme(plot.title=element_text(size=rel(2), hjust=0.5, vjust=1.5, face="bold"),
legend.text=element_text(size=17),
legend.position="left",legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.4), hjust=0.5, vjust=1),
panel.background = element_rect(fill = "white", colour = "gray95"),
strip.text = element_text(size=18),
axis.text.x = element_text(size=16),
axis.text.y = element_text(size=16))
ggmap_byscen
Result:
Question: I am looking to add an additional legend defined by the column "label" in the dataframe to identify the region on the map. Preferably, I'd like the legend to be below the faceted map. I have seen examples where one can add a table entry as a separate plot and then merge the two. I could not figure out how to make it for my case.
Any help would be great, thanks.
As #jlhoward mentioned, longlimits and latlimits are not defined. I, therefore, decided to leave coord_fixed(xlim = longlimits, ylim = latlimits) part from this answer. My workaround works, but I am sure there are better ways to work on this. The challenge was to create another legend in a way it can present the data well. If you use colour in geom_text, you can create another legend, but you end up seeing the alphabet, a in the grey boxes in the legend. So, I decided to use geom_point with alpha = 0 as well as colour in aes. In this way, you have a new legend with ID names, but you do not see any points on the maps. Then, I used annotate to assign the numbers on the maps. Thanks to #jlhoward, I created a small data frame which is necessary for annotate(). If you use the original data frame, R tries to write the texts 4000 times or so. In the theme part, I added legend.key = element_rect(fill = NA) in order to remove grey squares in the legend. I made the height and width of the figure pretty small so that I could post it here. So it does not look that great. But if you specify large numbers, the figure will look better.
library(dplyr)
library(ggplot2)
wmap_byscen.df <- read.csv("mydata.csv", header = T)
mydf <- wmap_byscen.df[wmap_byscen.df$variable != c("AVG") &
wmap_byscen.df$ID_1 != c("0"),]
### This is for annotate()
mydf2 <- select(mydf, c.long, c.lat, ID_1, ID_name) %>%
distinct()
### Color setting
palette = brewer.pal(11,"RdYlGn")
ggplot(mydf, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = value)) +
facet_wrap(~ variable) +
geom_path(colour = "grey50", size = .1) +
geom_point(aes(x = c.long, y = c.lat, color=factor(ID_name, levels=unique(ID_name)), label = ID_1), size = 1, alpha = 0) +
annotate("text", x = mydf2$c.long, y = mydf2$c.lat, label = mydf2$ID_1) +
scale_fill_gradientn(name = "% Change",colours = palette) +
scale_color_discrete(name = "Regions") +
#coord_fixed(xlim = longlimits, ylim = latlimits) +
scale_y_continuous(breaks = seq(-60,90,30), labels = c("60ºS","30ºS","0º","30ºN","60ºN","90ºN")) +
scale_x_continuous(breaks = seq(-180,180,45), labels = c("180ºW","135ºW","90ºW","45ºW","0º","45ºE","90ºE","135ºE","180ºE")) +
labs(x = "",y = "",title = "Average yield impacts across all crops across\nby climate scenarios (% change)") +
theme(plot.title = element_text(size = rel(2), hjust = 0.5, vjust = 1.5, face = "bold"),
legend.text = element_text(size = 8),
legend.position = "bottom",
legend.text = element_text(size = rel(1.3)),
legend.title = element_text(size = rel(1.4), hjust = 0.5, vjust = 1),
panel.background = element_rect(fill = "white", colour = "gray95"),
strip.text = element_text(size = 18),
axis.text.x = element_text(size = 16),
axis.text.y = element_text(size = 16),
legend.key = element_rect(fill = NA)) +
guides(col = guide_legend(nrow = 3, byrow = TRUE))

Resources