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
Related
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))
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"))
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))
I'm trying to create a simple point estimate with confidence interval plot. I can get it to plot as I'd like until I try to change the point shape and/or the color. When I try to change either I get "Warning: Removed 4 rows containing missing values (geom_point)." and end up with a blank plot.
I've checked out and tried the suggestions on:
here
here
here
and here
and a couple other places but to no avail.
A Reproducible Example
library(ggplot2)
set.seed(1)
# Create some sample data
point_est <- 4:1
se <- runif(4)
df <- data.frame(point_est = point_est,
se = se,
lower = point_est - se,
upper = point_est + se,
year = c("c", "c", "p", "p"),
group = letters[1:4])
group_names <- paste0("Display Name for \n Group ", LETTERS[1:4])
names(group_names) <- letters[1:4]
legend_text <- c("Previous Year Rate with 95% Confidence Intervals",
"Current Year Rate with 95% Confidence Intervals")
names(legend_text) <- c("p", "c")
df$year = factor(df$year, levels = names(legend_text), labels = legend_text)
df$group = factor(df$group, levels = names(group_names), labels = group_names)
# Plot looks good except the colors and shape of the points need changing
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
geom_point(size = 3.2) +
scale_x_discrete(drop=FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
labs(x = NULL,
y = "This is my Left Axis") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA),
panel.background = element_blank())
# now change the shapes of the points and the colors of the error bars
shapes <- c(17, 15)
names(shapes) <- names(legend_text)
colors <- c("pink", "blue")
names(colors) <- names(legend_text)
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
geom_point(size = 3.2) +
scale_x_discrete(drop=FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
scale_shape_manual(values = shapes) +
scale_color_manual(values = colors) +
labs(x = NULL,
y = "This is my Left Axis") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA),
panel.background = element_blank())
#> Warning: Removed 4 rows containing missing values (geom_point).
# Blank plot now and warnings:(
This is happening because you used names(legend_text) rather than legend_text as the names of your shapes and colors vectors. legend_text is what matches the values in the year column of your data. Do names(colors) <- legend_text and likewise for shapes and the plot will work. Nothing was plotted because the names of the colors and shapes vectors did not match any of the levels of df$year, so no colors or shapes were assigned for the actual values in year.
It looks like maybe you got tripped up by levels vs. labels in the factor function. By default, the levels are the existing set of unique values in the data and the labels are set equal to the levels. However, if you include a labels argument in factor, the data values get relabeled to be the values in the labels argument.
To make this concrete, note in the code below that the names of the shapes and colors vectors are p and c, which is different from the values in df$year.
> df[ , "year", drop=FALSE]
year
1 Current Year Rate with 95% Confidence Intervals
2 Current Year Rate with 95% Confidence Intervals
3 Previous Year Rate with 95% Confidence Intervals
4 Previous Year Rate with 95% Confidence Intervals
> levels(df$year)
[1] "Previous Year Rate with 95% Confidence Intervals" "Current Year Rate with 95% Confidence Intervals"
> shapes
p c
17 15
> colors
p c
"pink" "blue"
If you put the vectors directly into the ggplot it will work.
For scale_shape_manual put c(17,15) for the values and for scale_color_manual put c("Pink","Blue") for the values. Or just do not assign names to the shapes and colors vectors. That is what it is throwing it off.
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
geom_point(size = 3.2) +
scale_x_discrete(drop=FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
scale_shape_manual(values = c(17, 15)) +
scale_color_manual(values = c("pink", "blue")) +
labs(x = NULL,
y = "This is my Left Axis") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA),
panel.background = element_blank())
######if you want to use the vectors do not name them
shapes <- c(17, 15)
colors <- c("pink", "blue")
ggplot(df, aes(x = group, y = point_est, color = year, label= year, shape = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.3) +
geom_point(size = 3.2) +
scale_x_discrete(drop=FALSE) +
scale_y_continuous(sec.axis = sec_axis(~.*3, name = "This is my Right Axis")) +
scale_shape_manual(values = shapes) +
scale_color_manual(values = colors) +
labs(x = NULL,
y = "This is my Left Axis") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA),
panel.background = element_blank())
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)")))