Creating secondary legend in ggplot - r

I have a barplot overlaid with points to illustrate percent diesel emissions (points) and attributable percent decline per sector (bars). I want to create a secondary legend so I have two different ones (one for color fill in the barplot and the other for the shape). I cannot figure out the best way to create two separate legends instead of having one combined legend. Any help would be greatly appreciated!
c <- ggplot(diesel_sectorC,
aes(factor(Year), Percent, fill=Sector)) +
geom_bar(stat="identity", position= "dodge") +
xlab("Time Interval") +
ylab("Percent (%) of Attributable Decline") +
ggtitle("CA Percent Attributable Emissions") +
geom_point(aes(y = Total, grou = Sector),
stat = "identity", position = position_dodge(width = 0.9),
size = 3, color = "black", shape = 3) +
theme(plot.title = element_text(color="black", size=14, face="bold.italic"),
axis.title.x = element_text(color="#993333", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold")) +
scale_fill_manual("Sector",
values = c("HDDV" = "slateblue1", "Construction" = "steelblue1",
"LDDV" = "paleturquoise3", "Marine" = "thistle")) +
guides(fill=guide_legend(title="Attributable Decline Sectors"))

Related

What does fill=..level.. mean in ggplot?

I want to create a "heating map" for a dataset, which contains obs about shooting in NYC Boroughs.
ggmap(nyc_map)+
stat_density2d(data = NYPD,
aes(x = Longitude, y = Latitude, fill= ..level..),
alpha=0.08,
bins=30,
geom = "polygon") +
scale_fill_gradient(low = "red", high = "green", name="Shootings level") +
scale_alpha(range = c(0, 0.08), guide = "none") +
scale_size(range = c(0,0.75)) +
ggtitle("Shootings for Boroughs")+
theme(axis.ticks = element_blank(),
axis.text = element_blank(),
legend.position="right") +
theme(plot.title = element_text(hjust = 0.5))
In that case, what does fill=..level.. means?? Because I don't have a variable for the number of shootings, I have the number of rows and the lat and long for Boroughs.
Should fill=..level.. mean about mean of the shootings?
The dot-dot notation ..some_var.. allows to access statistics computed by ggplot to construct the plot. In this case, the levels for the 2d-density. You can, instead, extract and use them with stat(): see related question on RStudio community

plot TOTAL errorbar for multiple lines in ggplot r

I would like to plot the data by subject but adding the errorbar of the total mean and se. I mean, not an error bar for each subject. I've tried geom_errorbar and stat_summary but still failed to get my ideal plot (see the figure I drew).
and here is the code I used to draw this figure (the errorbars are added by hand).
ggplot(ASD, aes(x=period, y=meanF0, group=subject, color=group)) +
geom_line(aes(color=group, size=group)) +
scale_size_manual(values=c(.6, .6, .6, .6)) +
theme_light()+
xlab("Period")+
ylab("F0 (Hz)")+
ggtitle("Mean F0 Adjustment (ASD Group)") +
geom_point()+
scale_color_manual(values=c("red")) +
theme(plot.title = element_text(size=14.5, face="bold", hjust = 0.5, family = "serif"),
axis.title.y= element_text(size=12, face = "bold", family = "serif"),
axis.title.x= element_text(size=12, face = "bold", family = "serif"),
axis.text.x = element_text(size=11, face="bold", family = "serif"),
axis.text.y = element_text(size=11, face="bold", family = "serif"))+
theme(legend.position = "none")+
geom_hline(yintercept=112.8, linetype="dashed",
color = "dark grey", size=.7)
Anyone could help? Thank you very much!!!
Use annotate to add the error bars. I don't have your data, so I created my own. You're going to need the confidence interval and the average for each group. My average-by-group values and confidence interval-by-group are stored in df4$meanV and df4$ci. You can replace these with your variable names. In annotate, you'll include the data frame in the call like you would in base R plots. Like base R, you can just use raw values, as well. Multiple values can be joined with c(). As in y = c(12, 10). If you have any questions, just let me know.
ggplot(df2, aes(x = condition, y = value,
color = subject, group = subject)) +
geom_line() + geom_point() +
annotate("errorbar",
x = df4$condition
ymin = df4$meanV - df4$ci,
ymax = df4$meanV + df4$ci,
width = .2) +
annotate("point",
x = df4$condition,
y = df4$meanV) +
ylim(min(df2$value), max(df2$value))

Too many countries in the same plot

if someone could please help me, when I run the code bellow, it works, but there are too many countries in the graph, and the labels overlap so I wanted to:
a) learn any way to "stretch" the yaxis to that the country lines can be a bit more distant from one another, and consequently, the labels won’t overlap,
b) learn how to set a margin inside the graph, so that all the names from all the countries can fit in the graph frame (they are currently being cut).
r <- ggplot(df3, aes(x=year2, y=df_ji, colour=Country)) +
geom_line() +
scale_colour_discrete(guide = 'none') +
geom_dl(aes(label=Country), method =
list(dl.combine("first.points", "last.points"), cex = 1.0)) +
stat_summary(aes(y = df_ji, group=1), fun.y=mean, colour= "blue",
geom="line", alpha = .8, size = 1.5) +
theme_bw(base_size = 18, base_family = "serif") +
labs(title = "Judicial Independence across Latin America", x =
"Year", y = "Judicial Independence")
r + theme(plot.title = element_text(size=20, face="bold"),
axis.title.x = element_text(size=18),
axis.title.y = element_text(size=18))

Add secondary axis ggplot - line chart with two variables - one data frame

I used ggplot to make a like graph with two variables, but I need to add a secondary y-axis and assign it to one of the variables ("volt").
I also would like to specify the range of the secondary y-axis (upper and lower limit), as well as the breaks - as I did for the y-main-axis.
My two variables are "Sr" and "volt".
I don't want to use different dataframes and then merge the graphs.
Do any of you know how to do it?
Oh, I must add that I am an absolute beginner!
Thanks,
Pedro
ggplot(data = k, aes(x = Dist)) +
geom_line(aes(y = Sr), colour="blue") +
geom_line(aes(y = volt), colour = "grey") +
xlab(bquote('Distance-um')) +
ylab(bquote('Sr87Sr86')) +
geom_point(aes(y = Sr), colour="black", size=2) +
geom_point(aes(y = volt), colour="grey", size=2) +
theme(axis.title.x = element_text(colour="black",size=10,face="bold"),
axis.title.y = element_text(colour="black",size=10,face="bold"),
axis.text.x = element_text(colour="black",size=8, face="plain"),
axis.text.y = element_text(colour="black",size=8, face="plain")) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black", fill="transparent")) +
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) +
geom_hline(aes(yintercept=0.7061), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.7078), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.70467), colour="black", linetype="dotted") +
scale_x_continuous(limits=c(-0.01, 1000), breaks=c(0, 250, 500, 750, 1000))+
scale_y_continuous(limits=c(0.7039, 0.7101), breaks=c(0.7040, 0.7050,
0.7060, 0.7070, 0.7080, 0.7090)) +
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))
First, I would like to mention that two axis is not the best idea.
Having said that, if you still want two axis, you have to scale one of your variables (volt in this case).
Dist<-seq(1,10)
Sr<-c(0.704, 0.705, 0.706, 0.707, 0.708, 0.704, 0.705, 0.706, 0.707, 0.708)
volt<-c(3,5,10,8,12,4,11,3,14,22)
k<-data.frame(Dist,Sr,volt)
k$volt<-k$volt/10
Now, fixing the data makes things easier for plotting, just melt your variables
library(reshape)
k_melt<-melt(k,id="Dist")
And plotting. With sec_axis you can create the second axis and rescale again the values
ggplot(k_melt, aes(x=Dist, y=value, fill=variable, colour=variable))+
geom_line(stat='identity', size=0.5)+
geom_point(stat='identity', size=2)+scale_color_manual(values=c("blue", "grey")) +
scale_y_continuous("SR", sec.axis = sec_axis(~ . *10, name = "Volt"))`
NOTE: You can add your theme and geom_hline to this code. they don't work for the simulated data I created

ggplot2, facet wrap, fixed y scale for each row, free scale between rows

I would like to produce a plot using facet_wrap that has a different y scale for each row of the wrap. In other words, with fixed scales on the same row, free scales on different rows, with a fixed x scale. Free scales doesn't give me exactly what I'm looking for, nor does facet_grid. If possible, I'd like to avoid creating 2 separate plots and then pasting them together. I'm looking for a result like the plot below, but with a y scale max of 300 for the first row, and an y scale max of 50 in the second row. Thanks for any help!
Here is my code:
library(ggplot2)
library(reshape)
# set up data frame
dat <- data.frame(jack = c(150,160,170),
surgeon = c(155,265,175),
snapper = c(10,15,12),
grouper = c(5,12,50))
dat$island<-c("Oahu","Hawaii","Maui")
df<-melt(dat)
# plot
ggplot(df, aes(fill=variable, y=value, x=island)) +
geom_bar(width = 0.85, position= position_dodge(width=0.5),stat="identity", colour="black") +
facet_wrap(~variable, scales = "free_y",ncol=2) +
theme_bw() +
theme(strip.text = element_text(size=15, face="bold"))+
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "white", size = 0.2))+
theme(panel.grid.minor = element_line(colour = "white", size = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5, size=18))+
labs(y = expression(paste("Yearly catch (kg)")))
Drawing on one of the lower ranked answers from the link Eric commented, you can add a layer that blends into the background to enforce the axes.
Here I created a second data frame (df2) that puts a single point at "Hawaii" and the max value you wanted (300 or 50) for the four variable/fish types. By manually setting the color of the geom_point white, it fades into the background.
library(ggplot2)
library(reshape)
# set up data frame
dat <- data.frame(jack = c(150,160,170),
surgeon = c(155,265,175),
snapper = c(10,15,12),
grouper = c(5,12,50))
dat$island<-c("Oahu","Hawaii","Maui")
df<-melt(dat)
#> Using island as id variables
df2 <- data.frame(island = rep("Hawaii",4), variable = c("jack","surgeon","snapper","grouper"),value = c(300,300,50,50))
ggplot(df, aes(fill=variable, y=value, x=island)) +
geom_bar(width = 0.85, position= position_dodge(width=0.5),stat="identity", colour="black") +
geom_point(data = df2, aes(x = island, y = value), colour = "white") +
facet_wrap(~variable, scales = "free_y",ncol=2) +
theme_bw() +
theme(strip.text = element_text(size=15, face="bold"))+
theme(legend.position="none")+
theme(panel.grid.major = element_line(colour = "white", size = 0.2))+
theme(panel.grid.minor = element_line(colour = "white", size = 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust =1, vjust =0.5, size=18))+
labs(y = expression(paste("Yearly catch (kg)")))

Resources