Creating a linear gauge in R with ggplot2: reducing barplot width - r

I have created a linear gauge in R to be displayed within PowerBI.
My only issue is that the width of the plot cannot be adjusted so I am getting the following:
(Plot is being rendered in PowerBI)
Whereas I would like to obtain the same graph but half the width.
I tried using width within geom_bar but it resizes the bar and the final output is the same.
Ideally, the bar would be half its current width (I am building this graph for a PowerBI report).
This is the code I used:
library(ggplot2)
scores = factor(c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional'),
levels = (c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional')),
ordered = TRUE)
x <- data.frame(points = rep(1,7), scores= scores)
x %>%
ggplot(aes(x=points, fill=scores)) +
geom_bar(position = "stack", show.legend = FALSE) +
geom_text(aes(label=scores, y = seq(from=0.5, to=6.5, by = 1)), label.size = 0.25)+
coord_flip() +
theme(panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()) +
geom_point(aes(x= 1.45, y=5), shape = 25, size=10, colour = "black", fill = "black") +
geom_point(aes(x= 0.55, y=3), shape = 24, size=10, colour = "black", fill = "black") +
geom_point(aes(x= 0.55, y=6), shape = 24, size=10, colour = "black", fill = "black") +
scale_fill_brewer(palette = "RdYlGn", direction = -1)

If simply resizing the Power BI visual is no option, you can use theme(plot.margin = unit(c(0, 0.2, 0, 0.2), "npc")) for increasing margins that ggplot draws around plot. Full code:
library(tidyverse)
scores = factor(c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional'),
levels = (c('Inadequate','Adequate','Fair','Good','Great','Excellent','Exceptional')),
ordered = TRUE)
x <- data.frame(points = rep(1,7), scores= scores)
x %>%
ggplot(aes(x=points, fill=scores)) +
geom_bar(position = "stack", show.legend = FALSE) +
geom_text(aes(label=scores, y = seq(from=0.5, to=6.5, by = 1)), label.size = 0.25)+
coord_flip() +
theme(panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank()) +
geom_point(aes(x= 1.45, y=5), shape = 25, size=10, colour = "black", fill = "black") +
geom_point(aes(x= 0.55, y=3), shape = 24, size=10, colour = "black", fill = "black") +
geom_point(aes(x= 0.55, y=6), shape = 24, size=10, colour = "black", fill = "black") +
scale_fill_brewer(palette = "RdYlGn", direction = -1) +
theme(plot.margin = unit(c(0, 0.2, 0, 0.2), "npc"))

Related

Can you shift the position of a facet label or strip bar in ggplot?

I'm using ggplot to graph a forest plot. I have used facet labels to label groups (in example below Test1, Test2, Test3). Is there a way to slightly shift the actual position of the facet label/strip to the left (as indicated by the arrows in my picture below)?
I can shift the position of the text within the facet label but I think I have done that as much as possible. Thus, I think I need to shift the actual facet label (strip bar/rectangle) itself. Is this possible?
Would be very grateful if anyone could help me or point out a way to get a similar effect!
Please find reproducible code here:
library(dplyr)
library(ggplot2)
library(ggforce)
library(tidyverse)
# Reproducible dataset
df <- data.frame(outcome = c('outcome1', 'outcome1', 'outcome2','outcome2','outcome3','outcome3','outcome4','outcome4','outcome5','outcome5'),
type = c('Test1','Test1','Test2','Test2', 'Test3', 'Test3', 'Test3','Test3', 'Test3', 'Test3'),
Coef = c(0.10026935, 0.10026935, 0.13713358, 0.13713358,0.07753188,0.07753188,0.09193794,0.09193794,0.06170916,0.06170916),
CIr_low = c(0.070955475,0.070955475,0.108705781,0.108705781,0.052595474,0.052595474,0.056340327,0.056340327,0.036185918,0.036185918),
CIr_high = c(0.12958323,0.12958323,0.16556139,0.16556139,0.10246828,0.10246828,0.12753555,0.12753555,0.08723240,0.08723240),
model = c(1,2,1,2,1,2,1,2,1,2))
# Set type as factor
df <- df %>% mutate(type = fct_relevel(type, "Test1","Test2","Test3"))
# Plot with ggplot
ggplot(df, aes(x = outcome, y = Coef, ymin = CIr_low,ymax =CIr_high,fill = as.factor(type))) +
geom_errorbar(aes(x= outcome, ymin=CIr_low, ymax=CIr_high), width=0.2,cex=0.5)+
geom_point(shape = 18, size = 5)+
facet_grid(type ~ ., scales = "free", space = "free") +
geom_hline(yintercept = 0, linetype = 'dashed', col = 'black') +
scale_y_continuous(limits = c(-0.1, 0.25)) +
ggforce::facet_col(facets = type ~ ., scales = "free_y", space = "free", strip.position = "top")+
theme_bw()+
coord_flip() +
xlab('Group')+
ylab(expression("Standardized" ~ beta *" (95%CI)"))+
theme(line = element_line(colour = "black", size = 0.5),
plot.margin = margin(0.5, 0.5, 0.5, 0.5, unit = "cm"),
strip.background = element_rect(colour = "white", fill="white"),
strip.text = element_text(colour = "black",face="italic"),
strip.text.x = element_text(size = 12,angle = 0,hjust = 0,face="bold.italic", color="darkblue"),
legend.position ="none",
axis.line.x = element_line(colour = "black"),
axis.line.y = element_blank(),
panel.border= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
panel.spacing = unit(2, "lines"),
axis.ticks = element_blank(),
axis.title.x = element_text(colour = "black"),
axis.title.y = element_blank(),
axis.text=element_text( color = "black")
)
You can try:
ggplot(df, aes(x = outcome, y = Coef, ymin = CIr_low,ymax =CIr_high,fill = as.factor(type))) +
geom_errorbar(aes(x= outcome, ymin=CIr_low, ymax=CIr_high), width=0.2,cex=0.5)+
geom_point(shape = 18, size = 5, show.legend = F)+
geom_hline(yintercept = 0, linetype = 'dashed', col = 'black') +
scale_y_continuous(expression("Standardized" ~ beta *" (95%CI)"),limits = c(-0.1, 0.25)) +
xlab("")+
coord_flip() +
facet_grid(type~., scales = "free", space = "free_y", switch = "y") +
theme_minimal() +
theme(strip.placement = "outside",
strip.text.y.left = element_text(angle = 0,vjust = 1,size=12))
Or use a cowplot approach with ggtitle
plots <- df %>%
split(.$type) %>%
map2(.,names(.), ~ggplot(.x, aes(x = outcome, y = Coef, ymin = CIr_low,ymax =CIr_high,fill = as.factor(type))) +
geom_errorbar(aes(x= outcome, ymin=CIr_low, ymax=CIr_high), width=0.2, size=0.5)+
geom_point(shape = 18, size = 5, show.legend = F)+
geom_hline(yintercept = 0, linetype = 'dashed', col = 'black') +
scale_y_continuous(limits = c(-0.1, 0.25))+
coord_flip() +
xlab('')+
ylab(expression("Standardized" ~ beta *" (95%CI)"))+
ggtitle(.y)+
theme_minimal(base_size = 12)+
theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title.position = "plot"))
cowplot::plot_grid(plots$Test1 + theme(axis.title.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_blank()),
plots$Test2 + theme(axis.title.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_blank()),
plots$Test3, ncol = 1)

ggplot2: negative bar chart with dual x axis

I want to produce chart like this:
I go with this code:
dat <- data.frame(
name1 = c("A", "B", "C", "D", "E"),
name2 = c("F", "G", "H", "I", "J"),
value = c(-12,10,5,-7,-2)
)
ggplot(dat,aes(x = name1,y = value)) +
geom_bar(stat = "identity",
fill = "#465978",
width = 0.3) +
ylim(-50,50) +
geom_hline(yintercept = 0,
color = "#9e9e9e",
alpha = 0.3,
linetype = "dotted") +
coord_flip() +
theme_bw() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(colour = "#737373"),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
and ended up like this: (note: there is a line in the middle, if you can't see, may be due to low resolution)
How to add dat$name2 in the right side (i.e. top x axis)?
If you don't mind, maybe you can help me produce more similar chart?
Offering an alternative to r2evans' solution, you can convert a discrete variable to a continuous one by doing match(x, unique(x)). If you then have continuous variables on both axes, it is easy to add a secondary axis.
Here is how you could do that (with some extra decorations based on your request to make the chart more similar).
library(ggplot2)
dat <- data.frame(
name1 = c("A", "B", "C", "D", "E"),
name2 = c("F", "G", "H", "I", "J"),
value = c(-12,10,5,-7,-2)
)
# Probably easiest to define `cont_name1 = match(name1, unique(name1))` in the data
# instead of having to declare it in the `aes()` every time.
ggplot(dat,aes(x = value,y = match(name1, unique(name1)))) +
geom_tile(width = Inf, height = 0.3, fill = "grey95") +
geom_bar(stat = "identity",
fill = "#465978",
width = 0.3, orientation = "y") +
geom_segment(aes(y = match(name1, unique(name1)) - 0.15,
yend = match(name1, unique(name1)) + 0.3,
xend = value)) +
geom_text(aes(x = ifelse(value < 0, value - 4, value + 4),
y = match(name1, unique(name1)) + 0.2,
label = scales::percent(value, scale = 1, accuracy = 1)),
vjust = 0) +
xlim(-50,50) +
scale_y_continuous(
breaks = match(dat$name1, unique(dat$name1)),
labels = dat$name1,
sec.axis = sec_axis(~ .x, labels = dat$name2, breaks = 1:5)
) +
geom_vline(xintercept = 0,
color = "#9e9e9e",
alpha = 0.3,
linetype = "dotted") +
ggtitle("Title here (0%)") +
theme_bw() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(colour = "#737373"),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, colour = "grey60"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
Created on 2021-01-17 by the reprex package (v0.3.0)
Normally (with a continuous scale) I'd recommend scale_y_continuous(sec.axis=...). Unfortunately, scale_discrete does not yet support it (see https://github.com/tidyverse/ggplot2/issues/3171). With that, the two ways to go include geom_text and annotations. I'll offer the first.
To make it consistent on both sides, we'll need to remove the axis labels from the left axis. (I find this annoying, but consistency between axes is important to me. If it is not as much to you, then you can reduce some of the changes.)
ggplot(dat,aes(x = name1,y = value)) +
geom_bar(stat = "identity",
fill = "#465978",
width = 0.3) +
ylim(-50,50) +
geom_hline(yintercept = 0,
color = "#9e9e9e",
alpha = 0.3,
linetype = "dotted") +
geom_text(aes(y = -Inf, label = name1), hjust = 0, colour = "#737373") + # new
geom_text(aes(y = Inf, label = name2), hjust = 1, colour = "#737373") + # new
coord_flip() +
theme_bw() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(), # change
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
One problem you will run into with multi-line labels (as in your example) is that of alignment. ggplot2 has a confounded sense of hjust=, where it means both direction of the text box from the point and alignment of the text within the text box. So hjust=0 means "text goes to the right of the point, and the text is left-aligned". There does not seem to be an easy way to have the text box go to the right of the point (first geom_text, on the left-edge) yet have the text right-justified. (I'll be happy if somebody can show an easy way to work around this!)
The workarounds to force that left-edge right-align textbox require knowing a priori the dimensions of the plot so that you can hard-position the text box (not y=-Inf, the left-edge) within the plot boundary and hard-code the limits of the plot. (Know that when you form the plot, none of the functions know what the dimensions of the rendered plot will be, in user-points, centimeters, or similar.)
As an aside, we can add a geom_text and geom_tile for a couple more features.
ggplot(dat,aes(x = name1,y = value)) +
geom_tile(height = 90, width = 0.3, fill = "gray90") +
geom_bar(stat = "identity",
fill = "#465978",
width = 0.3) +
ylim(-50,50) +
geom_hline(yintercept = 0,
color = "#9e9e9e",
alpha = 0.3,
linetype = "dotted") +
geom_text(aes(y = -Inf, label = name1), hjust = 0, colour = "#737373") +
geom_text(aes(y = Inf, label = name2), hjust = 1, colour = "#737373") +
geom_text(aes(label = value), vjust = -1.1) +
coord_flip() +
theme_bw() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
. + > ggplot(dat,aes(x = name1,y = value)) +
geom_tile(aes(y = 0), height = 90, width = 0.3, fill = "gray90") +
geom_bar(stat = "identity",
fill = "#465978",
width = 0.3) +
ylim(-50,50) +
geom_hline(yintercept = 0,
color = "#9e9e9e",
alpha = 0.3,
linetype = "dotted") +
geom_text(aes(y = -Inf, label = name1), hjust = 0, colour = "#737373") +
geom_text(aes(y = Inf, label = name2), hjust = 1, colour = "#737373") +
geom_text(aes(label = value), vjust = -1.1) +
coord_flip() +
theme_bw() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())

Aligning text on horizontal bar chart

I've come across several threads pointing out how to annotate bar charts, but I've tried a number of iterations of this code and can't seem to get the text left justified, starting at 0% on the x axis. I've tried to change hjust to "left", 0.95, and progressively larger numbers - none of them have the text tethered to the x origin.
dummy_data <- tibble(Proportion = c(0.87, 1),
`Person of Interest` = c("Person B", "Person A"))
dummy_data %>%
ggplot(aes(x = Proportion, y = `Person of Interest`,
fill = `Person of Interest`,
label = paste0(`Person of Interest`, "~", scales::percent(Proportion))))+
geom_col(width = 0.5) +
geom_text(position = position_dodge(width = .9), # move to center of bars
vjust = 0, # nudge above top of bar
hjust = "top",
size = 4.5,
colour = "white",
fontface = "bold") +
scale_x_continuous(labels = scales::percent,
limits = c(0, 1.01),
expand = c(0, 0)) +
ggthemes::theme_economist(horizontal = F) +
scale_fill_manual(values = alpha(c("black", "#002D62"), .5)) +
ggtitle("Lack of Skill") +
theme(title = element_text("Lack of Skill"),
plot.title = element_text(hjust = 0.5, face = "italic"),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(hjust = 0.25),
legend.position="none",
aspect.ratio = 1/3)
I've often found text data with ggplot maddening - a huge thanks to anyone willing to take a look.
Try this approach that is close to what you want. Your themes can be producing the issues with placing the labels:
#Code
dummy_data %>%
ggplot(aes(x=`Person of Interest`,
y=Proportion,
fill=`Person of Interest`,
label = paste0(`Person of Interest`, "~", scales::percent(Proportion))))+
geom_bar(stat = 'identity')+
geom_text(aes(y=0.13),
size = 4.5,
colour = "white",
fontface = "bold")+coord_flip()+
scale_y_continuous(labels = scales::percent,
limits = c(0, 1.01),
expand = c(0, 0)) +
ggthemes::theme_economist(horizontal = F) +
scale_fill_manual(values = alpha(c("black", "#002D62"), .5)) +
ggtitle("Lack of Skill") +
theme(title = element_text("Lack of Skill"),
plot.title = element_text(hjust = 0.5, face = "italic"),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_text(hjust = 0.25),
legend.position="none",
aspect.ratio = 1/3)
Output:

Make the faceted x-axis text be the grouping factor in R using ggplot2 when plotting boxplots

I am creating faceted box plots that are grouped by a variable. Instead of having the x-axis text be the factors for the x-axis variable I'd like the x-axis text to be the grouping variable.
However, I don't just want to use the grouping variable as my x-axis variable because I'd like the boxplots to cluster. Its hard to explain well. But I think its clear from the code and comments below.
Let me know if you have any suggestions or can help and thanks in advance!
library(ggplot2)
library(scales)
ln_clr <- "black"
bk_clr <- "white"
set.seed(1)
# Creates variables for a dataset
donor = rep(paste0("Donor",1:3), each=40)
machine = sample(rep(rep(paste0("Machine",1:4), each=1),30))
gene = rep(paste0("Gene",LETTERS[1:5]), each=24)
value = rnorm(24*5, mean=rep(c(0.5,10,1000,25000,8000), each=24),
sd=rep(c(0.5,8,900,9000,3000), each=24))
# Makes all values positive
for(m in 1:length(value)){
if(value[m]<0){
value[m] <- sqrt(value[m]*value[m])
}
}
# Creates a data frame from variables
df = data.frame(donor, machine, gene, value)
# Adds a clone variable
clns <- LETTERS[1:4]
k=1
for(i in 1:nrow(df)/4){
for(j in 1:length(clns)){
df$clone[k] <- paste(df$donor[k],clns[j],sep="")
k = k+1
}
}
df$clone <- as.factor(df$clone)
#*************************************************************************************************************************************
# Creates the facet of the machine but what I want on the x-axis is clone, not donor.
# However, if I set x to clone it doesn't group the boxplots and its harder to read
# the graph.
bp1 <- ggplot(df, aes(x=donor, y=value, group=clone)) +
stat_boxplot(geom ='errorbar', position = position_dodge(width = .83),
width = 0.25, size = 0.7, coef = 1) +
geom_boxplot(coef=1, outlier.shape = NA, position = position_dodge(width = .83),
lwd = 0.3, alpha = 1, colour = ln_clr) +
geom_point(position = position_dodge(width = 0.83), size = 1.8, alpha = 0.9,
mapping=aes(group=clone)) +
facet_wrap(~ machine, ncol=2, scales="free_x")
bp1 + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = "black", angle=45, hjust=1),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1))
# Creates the facet of the Donor and clusters the clones but doesn't facet the
# machine. This could be okay if I could put spaces in between the different
# machine values but not the donors and could remove the donor facet labels, and
# only have the machine values show up once.
bp2 <- ggplot(df, aes(x=clone, y=value)) +
stat_boxplot(geom ='errorbar', position = position_dodge(width = .83),
width = 0.25, size = 0.7, coef = 1) +
geom_boxplot(coef=1, outlier.shape = NA, position = position_dodge(width = .83),
lwd = 0.3, alpha = 1, colour = ln_clr) +
geom_point(position = position_dodge(width = 0.83), size = 1.8, alpha = 0.9) +
facet_wrap(machine ~ donor, scales="free_x", ncol=6)
bp2 + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = "black", angle=45, hjust=1),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
panel.spacing = unit(0, "lines"))
Below is an example comparing what I'd like in an ideal world (Top two facets) as compared to what I'm getting (bottom two facets).
I'm not sure I understand exactly what you're trying to do, so let me know if this is on the right track:
library(dplyr)
pd = position_dodge(width=0.83)
ggplot(df %>% mutate(clone=gsub("Donor[1-3]","",clone),
donor=gsub("Donor", "", donor)),
aes(x=clone, y=value, color=donor, group=interaction(clone,donor))) +
geom_boxplot(coef=1, outlier.shape=NA, position=pd, lwd=0.3) +
geom_point(position=pd, size=1.8, alpha=0.9) +
facet_wrap(~ machine, ncol=2, scales="free_x") +
scale_y_log10(expand = c(0.02, 0)) +
theme(strip.background=element_rect(colour=ln_clr, fill=bk_clr, size=1))
How about this:
ggplot(df, aes(x=clone, y=value, group=interaction(clone,donor))) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
facet_wrap(~ machine, ncol=2, scales="free_x") +
scale_y_log10(expand = c(0.02, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = "black", angle=45, hjust=1),
strip.background=element_rect(colour=ln_clr, fill=bk_clr, size=1))
I found a work around for this problem but its not very elegant. I'd be super happy if some one came up with a better solution. Using the code to create a function for a "multiplot" found here and adding the code below I was able to do what I wanted. However, This is a slightly wonky solution in that I can't really format my titles with boxes around them and there are still two "clone" titles on the x axis that I can't replace easily with a single x-axis title. Also, had I of had many "machines" in my example this solution would have been painful to scale. All-in-all not ideal but passible for what I need. Special thanks to Eipi10 for their help, I appreciate it.
# Creates a multi-plot function for use in the graphs below
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
# Call multiplot function after storing each of the below plots as variables
ln_clr <- "black"
bk_clr <- "white"
bp3 <- ggplot(df[df$machine=="Machine1",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 1") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), color = ln_clr, angle=45, hjust=1),
panel.spacing = unit(0.25, "lines"), axis.title.x= element_blank(),
plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
axis.line.y= element_line(size = 1.25, colour = ln_clr),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
bp4 <- ggplot(df[df$machine=="Machine2",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 2") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = ln_clr, angle=45, hjust=1),
panel.spacing = unit(0.25, "lines"), plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
axis.line.y= element_line(size = 1.25, colour = ln_clr),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
bp5 <- ggplot(df[df$machine=="Machine3",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 3") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(panel.spacing = unit(0.25, "lines"), axis.title.y= element_blank(),
axis.title.x= element_blank(),axis.line.y= element_blank(),
axis.text.y=element_blank(),
axis.text.x= element_text(size=rel(1), colour = ln_clr, angle=45, hjust=1),
axis.ticks.y=element_blank(), plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
bp6 <- ggplot(df[df$machine=="Machine4",], aes(x=clone, y=value)) +
geom_boxplot(coef=1, outlier.shape=NA, lwd=0.3) +
geom_point(size=1.8, alpha=0.9) +
ggtitle("Machine 4") +
expand_limits(y=c(0.001,10^5)) +
facet_wrap(~ donor, nrow=1, scales="free_x") + scale_y_log10(expand = c(0, 0)) +
theme(axis.text.x= element_text(size=rel(1), colour = ln_clr, angle=45, hjust=1),
panel.spacing = unit(0.25, "lines"), plot.title = element_text(hjust=0.5),
strip.text.x = element_text(size=rel(1), face="bold", colour = ln_clr),
strip.background = element_rect(colour = ln_clr, fill = bk_clr, size = 1),
axis.line.x= element_line(size = 1.25, colour = ln_clr),
axis.line.y= element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title.y= element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = bk_clr),
panel.border = element_blank(),
plot.background = element_rect(fill = bk_clr))
# Plot all 4 graphs and saves them as a output file
png(filename="graph3.png", width= 9, height= 7.5, units = "in", res=600)
multiplot(bp3, bp4, bp5, bp6, cols=2)
dev.off()
Alternatively, if I set the "strip.text.x = " and the "strip.background =" as element_blank(). I can generate the below:

Add legend to ggplot object (why two legends?)

I created a ggplot2 object:
a <- replicate(8,rnorm(100))
colnames(a) <- letters[1:8]
b < -melt(a,id.vars=1:1)
colnames(b) <- c("c","variable","value")
ggplot(b,aes(x = c,y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = factor(variable)), size = 1.7) +
scale_x_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand=c(0.01, 0.01)) +
scale_y_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand = c(0.01, 0.01))+
theme_bw(base_size = 12, base_family = "Helvetica") +
theme(axis.text=element_text(size = 10),
axis.title=element_text(size = 10),
text = element_text(size = 10),
axis.line = element_line(size = 0.25),
axis.ticks=element_line(size = 0.25),
panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size = 0.5),
panel.background = element_blank(),
legend.position = "top" ,
legend.direction = "vertical",
legend.title = element_blank(),
legend.text = element_text(size = 13),
legend.background = element_blank(),
legend.key = element_blank()) +
labs(x = '', y = '', title = "") +
theme(plot.title = element_text(size=10)) +
theme(strip.text.x = element_text(size = 8,color="black"),
strip.background = element_blank()) +
theme(strip.text.x = element_text(size = 8, colour = "black"))
My problem is the following:
when I create the legend, there is a separate legend for the colors and a separate one for the points.
How can I create a single legend for each of the 8 variables?
Let me minimise your code and focus on the legend issue. This is what you have now.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line() +
geom_point(aes(shape = factor(variable)),size=1.7)
Your data frame, b has variable as factor. You use this in two ways here; variable and factor(variable). You can simply use variable for shape in geom_point; make all variable identical.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = variable),size = 1.7)
I saw some warning messages related to colours and other things. You may want to take care of them. But, for legend, this is one way to go.
Take from the ideas on this page: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels
I edited your code to make the data visible (you had problems with your x-axis limits. Note the final three lines. These commands tell ggplot to create only one legend.
a<-replicate(6,rnorm(100))
colnames(a)<-letters[1:6]
b<-melt(a,id.vars=1:1)
colnames(b)<-c("c","variable","value")
ggplot(b,aes(x=c,y=value,colour=variable,linetype=variable)) +
geom_line() + geom_point(aes(shape=factor(variable)),size=1.7)+
scale_x_continuous(limits=c(0,100))+
scale_y_continuous(limits=c(-2,2),breaks=seq(-2,2,0.1),expand=c(0.01,0.01))+
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
text = element_text(size=10),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
panel.border = element_rect(colour="black",fill=NA,size=0.5),
panel.background = element_blank(),
legend.position="top" ,
legend.direction="vertical",
legend.title=element_blank(),
legend.text=element_text(size=13),
legend.background=element_blank(),
legend.key=element_blank())+
labs(x='', y='',title="")+
theme(plot.title=element_text(size=10))+
theme(strip.text.x = element_text(size = 8,color="black"),strip.background=element_blank())+
theme(strip.text.x = element_text(size = 8,color="black"))+
scale_colour_discrete(name ="Factor")+
scale_linetype_discrete(name ="Factor") +
scale_shape_discrete(name ="Factor")

Resources