Unable to customize strip in facet_grid - r

I am trying to customize the strip in facet_grid function.
My graph currently looks like:
I successfully changed the background of strip to purple, however, I could not change the border color to black, even though I set the color to 'black' in the function.
Also, I want the space between text and the rectangle to be larger so that it will looks nicer. How should I achieve this?
My codes looks like:
plot.density <- ggplot(df_densityWindow, aes(x = idx, y = density, color = factor(location))) +
geom_bar(stat = 'identity', fill = 'white') +
facet_grid(marker ~ case, scales = 'free') +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF")) +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
background_grid(major = 'y', minor = "none") + # add thin horizontal lines
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(axis.title = element_text(size = 28)) +
theme(axis.text = element_text(size = 26)) +
theme(legend.text = element_text(size = 16)) +
theme(legend.title = element_text(size = 18)) +
panel_border() # and a border around each panel
plot(plot.density)
If necessary, the data could be downloaded here:
data
Thank you!

Your colour specification for strips works without error for me. The spacing between the strip text and box can be increased by setting the margin argument at the strip.text theme:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
facet_grid(Species ~ rev(Species)) +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)))
Possible debug strategy:
Reproduce code above
1A. If that doesn't work, check if your ggplot versions is ~3.2-ish
1B. If that does work, proceed to 2.
Try to remove and place back lines of plotting to see where the error occurs
I'm particularly unfamiliar with panel_border() and background_grid(), so you could try those first.
EDIT: Plot based on data and code provided
ggplot(data, aes(x = idx, y = density, colour = factor(location))) +
geom_col(fill = "white") +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
facet_grid(marker ~ case, scales = "free") +
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(strip.background = element_rect(colour = "red", fill = "#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)),
axis.title = element_text(size = 28),
axis.text = element_text(size = 26),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
# Improvised based on missing functions
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey90"),
panel.background = element_rect(fill = "white", colour = NA),
panel.border = element_rect(colour = "grey90", fill = NA))

Related

Draw a vertical line from x axis straight up ggplot

I am trying to plot my ggplot with a vertical line that goes from 0 on the x-axis and straight up. I have tried to use geom_line, but I don't seem to get it to work. I would like something similar to the figure. This is my code so far.
ggplot(dat_plot, aes(x = values, y = ind, fill = "lightgrey", width = 0.5)) +
stat_summary(fun.data = quantiles_95, geom = "boxplot") +
ggtitle("Wild boar") +
ylab("") +
xlab("Effect") +
scale_y_discrete(labels = faktorer_vs) +
theme_classic() +
theme(legend.position = "none", axis.text = element_text(size = 10),
axis.title = element_text(size = 13), axis.line = element_line(colour = "black"),
plot.title = element_text(size = 18))

Changing the figure legend to indicate the line type... (ggplot2)

I have the formula below:
ggplot(Errortrialsmodifyoriginal, aes(x = Target, y = Absolutefirststoperror, color = as.character(Type), shape = as.character(Type))) +
geom_point(shape=16)+
geom_point(data=Errortrialoriginal,shape=19,size = 4,mapping=aes(x=Target, y=Absolutefirststoperror)) +
geom_line(data=Errortrialoriginal,aes(group=Type,linetype=Type),size=2,) +
scale_color_manual(name = "Condition", values = c("red","green","blue","red","green","blue")) +
scale_linetype_manual(name = "Condition",values = c("dashed","dashed","dashed","solid","solid","solid")) +
geom_errorbar(data=Errortrialoriginal,mapping=aes(x=Target, ymin=Absolutefirststoperror-SE,ymax=Absolutefirststoperror+SE),size=0.5) +
theme_bw() + guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
theme(axis.title.x = element_text(size=14, face="bold"), axis.title.y = element_text(size=14, face="bold"),legend.text=element_text(size=14),title=element_text(size=14,face="bold"), plot.title = element_text(hjust = 0.5), legend.title = element_text(size=14,face="bold"), axis.text.x=element_text(size=14),axis.text.y=element_text(size=14),panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Which produces the graph:
How can I change my command to ensure that the dashed and solid lines are shown in the figure legend; because at the moment, the figure legend suggests that all the lines are solid, even though they are not?
I would be grateful for any advice!
To my opinion, the legend is correctly displayed but you can't see it because you have big points front of the linetype. You should increase the legend box to see it.
Here an example with this dummy example:
library(ggplot2)
ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
geom_line()+
geom_point(size = 4)
library(ggplot2)
ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
geom_line()+
geom_point(size = 4)+
theme(legend.key.size = unit(3,"line"))
So, with your code, you can do something like that:
library(ggplot2)
ggplot(Errortrialsmodifyoriginal,
aes(x = Target,
y = Absolutefirststoperror,
color = Type)) +
geom_point()+
geom_line(data=Errortrialoriginal,
aes(group=Type,
linetype=Type)) +
scale_color_manual(name = "Condition", values = rep(c("red","green","blue"),2)) +
scale_linetype_manual(name = "Condition",values = rep(c("dashed","solid"),each =3)) +
geom_errorbar(data=Errortrialoriginal,
mapping=aes(x=Target,
ymin=Absolutefirststoperror-SE,
ymax=Absolutefirststoperror+SE),size=0.5) +
theme_bw() +
guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
theme(axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"),
legend.text=element_text(size=14),
title=element_text(size=14,face="bold"),
plot.title = element_text(hjust = 0.5),
legend.title = element_text(size=14,face="bold"),
axis.text.x=element_text(size=14),
axis.text.y=element_text(size=14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.key.size = unit(3,"line"))
Does it answer your question ?
If not, please consider providing a reproducible example of your dataset (see: How to make a great R reproducible example)

Legend breaks cut off at the limits ggplot2

I'm plotting different types of graphs where my legend has the colorbar of the values in my graph in log like in the image below:
The code that gives something like this is:
2Dmap <- ggplot(x, aes(L, W, fill = R)) +
geom_raster() + coord_fixed() +
scale_fill_viridis(name="R", trans = "log", direction = -1, labels=trans_format('log10',math_format(10^.x)), breaks=cycle.breaks,
limits=c(10^(floor(log10(min(x$R[x$R > 0] )))), 10^(ceiling(log10(max(x$R)))) ) )+
theme_bw() + coord_fixed(ratio=12)
2Dwmap <- 2Dwmap + theme(legend.position="bottom") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.key.width=unit(2.2, "cm")) +
theme(legend.text=element_text(size=15)) +
theme(legend.position="bottom",
title = element_text(size = 20),
axis.title.x=element_text(size = 20),
axis.title.y=element_text(size = 20),
axis.text.x = element_text(size = 18),
axis.text.y = element_text(size = 18),
legend.title = element_text(size = 18),
legend.text = element_text(size = 16))
So, I'm trying to look for a good way to avoid my limiting breaks to be cut off.
Any1 any ideas ?
Sorry I cannot share data to make a reproducible example :)

How to change size of points of legend when 2 legends are present

I have a graph where two legends are present. I need to change the size of the points of one of the legends.
I need to change the bullet size of "market type" in the legend. I use the example here but does not work for my graph.
My code is below:
k <- ggplot(subsetdf) + theme_bw() +
geom_point( aes(y=y, x=x, size =Total.Unit.Count, fill = region), shape=21)+
scale_colour_hue(aes(y=y, x=x),l=50) + # Use a slightly darker palette than normal
geom_text_repel (aes(y=y, x=x, label = rownames(subsetdf))) +
geom_smooth(aes(x=x, y=y),method=lm, # Add linear regression lines
se=FALSE) +
labs(y = "title", x = "title",
title = "title",
size = "size", fill = "fill")+
theme(plot.title = element_text (face = 'bold',size = 21,hjust = 0.5),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
axis.text.x = element_text(size = 18,angle=45, hjust=1),
axis.text.y = element_text(size = 18,hjust = 1),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
scale_size_continuous(range = c(3,8))+
guides(colour = guide_legend(override.aes = list(size=10)))
You used the fill aesthetic guide, not color. So that is the guide to override.
Below is an example with iris dataset, as you code is not reproducible.
library(ggplot2)
ggplot(iris) +
geom_point(aes(Sepal.Length, Petal.Length, size = Sepal.Width, fill = Species), shape = 21) +
guides(fill = guide_legend(override.aes = list(size=8)))

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