Too many countries in the same plot - r

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))

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))

Creating secondary legend in ggplot

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"))

ggplot2: increasing space between categorical axis ticks with geom_point

I have a geom_point plot that with a large number of categorical variables, and a size parameter mapped to a continuous variable. When I make the plot, the categorical variables are too close together, and the large points from within each overlap. Is there any way to give a little breathing room to the axis so that this doesn't happen? I'm aware that an alternative solution is simply to use scale_size_area(max_size = 3) to narrow the range of point sizes, but I'd prefer not to do this as it makes it too difficult to tell them apart.
Here's the code:
plot <- ggplot(allcazfull, aes(x = Family, y = ifelse(Percentage==0,NA, Percentage), fill = Treatment, size = ifelse(Number == 0, NA,Number))) +
facet_wrap(~ Pathogen, scales = "free_x") +
geom_point(shape = 21) +
scale_fill_manual(values = alpha(c("#98fb98","#f77e17","#0d5a0d","#8d0707"),.6)) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
aspect.ratio = 4/1,
strip.background = element_rect(fill="white", linetype = "blank"),
strip.text = element_blank()) +
scale_x_discrete(limits = rev(levels(allcazfull$Family))) +
xlab("") +
ylab("") +
guides(fill = FALSE, size = FALSE) +
coord_flip()
plot
And here's the resulting figure:

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