Plotting labels of Points outside of Polygon with ggplot2/ggrepel - r

I am trying to do a map of my study areas using ggplot2 as I have seen some good outputs. I attached what I got from the following codes
The shapefile can be downloaded with this link.
library(ggplot2)
library(ggrepel)
points <- structure(list(city = structure(
c(
9L, 1L, 1L, 1L, 7L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 8L, 1L, 1L, 5L, 1L, 4L, 1L, 1L, 6L),
.Label = c("", "Adelaide", "Brisbane", "Canberra", "Darwin", "Hobart", "Melbourne", "Perth", "Sydney"),
class = "factor"),
site = structure(c(19L, 20L, 21L, 22L, 14L, 15L, 5L, 6L, 7L, 8L, 2L, 3L, 4L, 16L, 17L, 18L, 12L, 13L, 9L, 10L, 11L, 1L),
.Label = c("", "ADL1", "ADL2", "ADL3", "BNE1", "BNE2", "BNE3", "BNE4", "CBR1", "CBR2", "CBR3", "DRW1", "DRW2", "MEL1", "MEL2", "PER1", "PER2", "PER3", "SYD1",
"SYD2", "SYD3", "SYD4"), class = "factor"),
station = structure(c(8L, 5L, 12L, 16L, 10L, 3L, 22L, 18L, 17L, 20L, 2L, 14L, 11L, 4L, 7L, 19L, 15L, 21L, 6L, 9L, 13L, 1L),
.Label = c("", "Adelaide CBD", "Alphington", "Caversham", "Chullora", "Civic", "Duncraig", "Earlwood", "Florey", "Footscray", "Le Fevre 2", "Liverpool", "Monash", "Netley", "Palmerston", "Richmond", "Rocklea", "South Brisbane", "South Lake", "Springwood", "Winnellie", "Woolloongabba"), class = "factor"),
latitude = c(-33.9178, -33.8939, -33.9328, -33.6183, -37.8048, -37.7783, -27.4975, -27.4848, -27.5358, -27.6125, -34.9289, -34.9438, -34.7913, -31.9505, NA, NA, -12.50779, -12.4243233, -35.285307, -35.220606, -35.418302, -42.8821),
longitude = c(151.1347, 151.045, 150.9058, 150.7458, 144.8727, 145.0306, 153.035, 153.0321, 152.9934, 153.1356, 138.6011, 138.5492, 138.498, 115.8605, NA, NA, 130.94853, 130.8933502, 149.131579, 149.043539, 149.094018, 147.3272)),
.Names = c("city", "site", "station",
"latitude", "longitude"), class = "data.frame", row.names = c(NA, -22L))
AUS<-readRDS("gadm36_AUS_1_sp.rds")
ggplot() + geom_polygon(data = AUS, aes(x=long, y = lat, group = group, size=0.01),
fill = NA, color = "black") +
geom_point(data=points, aes(x=longitude, y=latitude), color ="blue", size=0.5) +
coord_fixed(1) +
geom_label_repel(data = points, aes(x=longitude, y=latitude, label=city),
box.padding = 1.2, point.padding = 1) +
theme_classic() + ylim(-60,0)+ xlim(100,180) + scale_size_identity()
Australia Map and my study areas:
Is there a way I can enlarge/expand the polygon so my points would be separated? Also can I direct all labels outside the polygon?

Related

Plot Y values against the time grouped by an ID

I want make a time series plot grouped by ID. My dataset has 42 different IDs with 7 different timeframes. The timeframe varies per ID and ranges from 9/2016 to 8/2018. I.e., ID1 can start 10/2016 and end 7/2017 (with 7 rows containing a different date) and ID40 can start 11/2016 and ends 6/2018 (also with 7 rows containing a different date). I try to plot this with the following code
p <- ggplot(data = df6, aes(x = START, y = AI, col = ID, group = ID))
p + geom_point(size = 1.2,
alpha = .8) + stat_smooth(aes(group = 1)) + stat_summary(aes(group = 1), geom =
"point", fun.y = mean,
shape = 17, size = 3) + theme_minimal() + theme(axis.text.x = element_text(angle =
90, vjust = 0.5, hjust=1))
This gives me the following graph:
As one can see the X-axis is not chronological. I should start at 09/2016 and end at 08/2018 and then correspond with the Y value based on the ID. I got the following dataset:
structure(list(ID = c("ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID10", "ID10", "ID10", "ID10", "ID10", "ID10", "ID10",
"ID11", "ID11", "ID11", "ID11", "ID11", "ID12"), Time = c("1",
"2", "3", "4", "5", "6", "7", "1", "2", "3", "4", "5", "6", "7",
"1", "2", "3", "4", "5", "1"), AI = c(0.393672183448241, 0.4876954603533,
0.411717908455957, 0.309769862660288, 0.149826889496538, 0.2448558592586,
0.123606753324621, 0.296109333767922, 0.309960002123076, 0.445886231347992,
0.370013553008003, 0.393414429902431, 0.318940511323733, 0.131112361225666,
0.31961673567578, 0.227268892979164, 0.433471105477564, 0.207184572401005,
0.144257239122978, 0.520204263001733), AI_VAR = c(0.154977788020905,
0.237846862049217, 0.169511636143347, 0.0959573678125739, 0.0224480968162077,
0.0599543918132674, 0.0152786294674538, 0.0876807375444826, 0.0960752029161373,
0.198814531305715, 0.136910029409606, 0.154774913655455, 0.101723049763444,
0.0171904512661696, 0.102154857724042, 0.0516511497159746, 0.187897199283942,
0.0429254470409874, 0.020810151039384, 0.270612475245176), activity = c(0,
0.303472222222222, 0.232638888888889, 0.228472222222222, 0.348611111111111,
0.215972222222222, 0.123611111111111, 0.357638888888889, 0.235416666666667,
0.233333333333333, 0.2875, 0.353472222222222, 0.356944444444444,
0.149305555555556, 0.448611111111111, 0.213888888888889, 0.248611111111111,
0.288888888888889, 0.25625, 0.238888888888889), ZIM_SD = c(0,
0.148002025121106, 0.095781596758851, 0.0707738088994687, 0.0522313184217097,
0.0528820640482116, 0.0152791681192935, 0.105900213118389, 0.0729697504998075,
0.104040120647865, 0.106378896489801, 0.139061072791901, 0.113844043625277,
0.0195758039329988, 0.143383618921218, 0.0486102909983211, 0.107765733167339,
0.059853320915846, 0.036965917525263, 0.124271018383747), ZIM_VAR = c(0,
0.0721799157746582, 0.039434998686126, 0.0219235930627339, 0.00782565597342798,
0.0129484832318932, 0.00188860836472692, 0.0313580415523671,
0.0226177040198407, 0.0463900573046668, 0.0393616334552618, 0.0547086326740462,
0.0363094774850072, 0.00256662987654616, 0.0458278042289798,
0.0110476070225835, 0.0467133314886466, 0.0124006847007297, 0.00533260120384214,
0.0646463135307921), CHECK = c(10L, 13L, 11L, 7L, 7L, 5L, 4L,
36L, 36L, 34L, 34L, 32L, 29L, 21L, 28L, 27L, 26L, 25L, 21L, 36L
), BULBAR = c(2L, 4L, 4L, 4L, 4L, 2L, 2L, 9L, 9L, 9L, 9L, 9L,
7L, 6L, 12L, 12L, 11L, 11L, 11L, 11L), FINE = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 9L, 9L, 8L, 8L, 7L, 6L, 4L, 2L, 1L, 1L, 1L, 0L, 7L
), GROSS = c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 9L, 9L, 9L, 9L, 8L,
8L, 6L, 3L, 3L, 3L, 3L, 2L, 6L), RESPI = c(6L, 7L, 5L, 1L, 1L,
1L, 1L, 9L, 9L, 8L, 8L, 8L, 8L, 5L, 11L, 11L, 11L, 10L, 8L, 12L
), GROSS_RENEWD = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L,
5L, 5L, 4L, 3L, 3L, 3L, 3L, 2L, 3L), ACTIVE = c(2L, 2L, 2L, 2L,
2L, 2L, 1L, 18L, 18L, 17L, 17L, 15L, 14L, 10L, 5L, 4L, 4L, 4L,
2L, 13L), NON.ACTIVE = c(8L, 11L, 9L, 5L, 5L, 3L, 3L, 18L, 18L,
17L, 17L, 17L, 15L, 11L, 23L, 23L, 22L, 21L, 19L, 23L), START = c("09/2016",
"11/2016", "01/2017", "04/2017", "06/2017", "10/2017", "02/2018",
"10/2016", "12/2016", "02/2017", "04/2017", "07/2017", "11/2017",
"04/2018", "10/2016", "12/2016", "02/2017", "04/2017", "07/2017",
"10/2016"), STOP = c("10/2016", "11/2016", "01/2017", "04/2017",
"06/2017", "10/2017", "03/2018", "10/2016", "12/2016", "02/2017",
"04/2017", "07/2017", "11/2017", "04/2018", "10/2016", "12/2016",
"02/2017", "04/2017", "07/2017", "10/2016")), row.names = c(NA,
20L), class = "data.frame")
In general I want the column START to start with the begin date and end with the last date when it is plotted
You should convert your "START" column to a date format. You could use the package zoo with the function as.yearmon for that. To start the axis with your start date and end it with the end date, you could create a vector of date breaks using the min (start) date and max (end) date. Here is a reproducible example:
library(ggplot2)
library(zoo)
library(dplyr)
df6 <- df6 %>%
mutate(START = as.Date(as.yearmon(START, format = '%m/%Y')))
breaks.vec <- c(min(df6$START),
seq(from=min(df6$START), to=max(df6$START), by = 'month'))
ggplot(data = df6, aes(x = START, y = AI, col = ID, group = ID)) +
geom_point(size = 1.2, alpha = .8) +
stat_smooth(aes(group = 1)) +
stat_summary(aes(group = 1), geom = "point", fun.y = mean, shape = 17, size = 3) +
scale_x_date(breaks = breaks.vec, date_labels = "%m/%Y") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
#> Warning: `fun.y` is deprecated. Use `fun` instead.
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Created on 2022-10-17 with reprex v2.0.2

Change x axis labels to hours (time) on geom_tile()

Here is a geom_tile displaying hours and days of the week, how can it made to display each hour (i.e. 00:00 through to 23:00 on the x axis)?
library(tidyverse)
df %>%
ggplot(aes(hour, day, fill = value)) +
geom_tile(colour = "ivory")
Currently it displays every fifth hour:
I have tried a bunch of different things, and would prefer a 'best practice' way (i.e. without manually generating labels), but in case labels are needed, here's one way to produce them hour_labs <- 0:23 %>% { ifelse(nchar(.) == 1, paste0("0", .), .) } %>% paste0(., ":00")
Data for reproducible example
df <- structure(list(day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Sunday",
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
), class = c("ordered", "factor")), hour = c(0L, 2L, 3L, 5L,
6L, 7L, 8L, 10L, 11L, 12L, 13L, 18L, 21L, 22L, 23L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 20L, 21L, 22L,
23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 13L, 14L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 20L, 21L, 22L, 23L, 0L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 11L, 13L, 14L, 15L, 16L,
19L, 21L, 0L, 1L, 2L, 3L, 7L, 8L, 10L, 13L, 14L, 22L, 23L), value = c(1L,
1L, 1L, 2L, 1L, 3L, 1L, 1L, 2L, 1L, 3L, 1L, 2L, 13L, 13L, 24L,
39L, 21L, 17L, 25L, 22L, 27L, 28L, 19L, 6L, 2L, 2L, 1L, 2L, 2L,
7L, 23L, 38L, 18L, 26L, 21L, 20L, 31L, 40L, 35L, 22L, 5L, 3L,
2L, 7L, 4L, 3L, 3L, 3L, 17L, 13L, 23L, 24L, 19L, 31L, 13L, 35L,
50L, 22L, 13L, 7L, 2L, 1L, 1L, 1L, 1L, 3L, 14L, 17L, 33L, 32L,
32L, 25L, 29L, 27L, 38L, 26L, 11L, 8L, 4L, 5L, 5L, 3L, 1L, 1L,
3L, 14L, 21L, 24L, 22L, 25L, 26L, 23L, 58L, 36L, 26L, 6L, 3L,
1L, 5L, 3L, 1L, 1L, 3L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 1L)), row.names = c(NA, -116L), groups = structure(list(day = structure(1:7, .Label = c("Sunday",
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
), class = c("ordered", "factor")), .rows = structure(list(1:15,
16:33, 34:51, 52:69, 70:88, 89:105, 106:116), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr"))), row.names = c(NA, 7L), class = c("tbl_df", "tbl",
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"))
Here's one way using sprintf to construct labels.
library(dplyr)
library(ggplot2)
df %>%
mutate(lab = sprintf('%02d:00', hour)) %>%
ggplot() + aes(lab, day, fill = value) +
geom_tile(colour = "ivory") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
To complete the missing times apart from #Eric Watt's suggestion we can also use complete.
df %>%
mutate(lab = sprintf('%02d:00', hour)) %>%
tidyr::complete(lab = sprintf('%02d:00', 0:23)) %>%
ggplot() + aes(lab, day, fill = value) +
geom_tile(colour = "ivory") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
I would suggest making sure your data type is correctly representing your data. If your hour column is representing time in hours, then it should be a time based structure. For example:
df$hour <- as.POSIXct(as.character(df$hour), format = "%H", tz = "UTC")
Then you can tell ggplot that the x axis is a datetime variable using scale_x_datetime.
ggplot(df, aes(hour, day, fill = value)) +
geom_tile(colour = "ivory") +
scale_x_datetime(labels = date_format("%H:%M")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
If you want a break for every hour, you can input that as breaks:
ggplot(df, aes(hour, day, fill = value)) +
geom_tile(colour = "ivory") +
scale_x_datetime(breaks = as.POSIXct(as.character(0:23), format = "%H", tz = "UTC"),
labels = date_format("%H:%M")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
You can also use the scales package which has handy formatting options such as date_breaks:
library(scales)
ggplot(df, aes(hour, day, fill = value)) +
geom_tile(colour = "ivory") +
scale_x_datetime(breaks = date_breaks("1 hour"),
labels = date_format("%H:%M")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

ggplot plotting vertical lines only?

When entering the following code, I get a weird ggplot where it plots vertical lines.
ggplot(data = otherdata, aes(x = subject, y = pct_.below)) + geom_point(aes(colour = subgroup))
When doing geom_point rather than geom_line, I get the other graph. I have no idea why this happens. There are more points than there are subgroups but that's not the solution to the issue. What do I do to fix this ggplot?
# dummy data
set.seed(45)
df <- data.frame(x=rep(1:5, 9), val=sample(1:100, 45),
variable=rep(paste0("category", 1:9), each=5))
# plot
ggplot(data = df, aes(x=x, y=val)) + geom_line(aes(colour=variable))
That code that I just posted works but I have no idea what the difference is between the two codes.
First 20 rows of the data:
structure(list(subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Algebra II",
"Biology I", "Chemistry", "English I", "English II", "English III",
"Geometry", "Int Math I", "Int Math II", "Int Math III", "US History"
), class = "factor"), pct_.below = c(0, 12.5, 12.4, 12.5, 0,
0, 12.5, 8.4, 11.1, 12.8, 11.9, 0, 11.5, 9, 100, 66.7, 100, 100,
100, 50), subgroup = structure(c(2L, 3L, 4L, 5L, 7L, 10L, 11L,
12L, 13L, 15L, 16L, 17L, 18L, 19L, 3L, 4L, 5L, 8L, 10L, 11L), .Label = c("All Students",
"Asian", "Black or African Amer", "Black/Hispanic/Native Amer",
"ED", "English Learner T 1-2", "English Learner T 1-4", "English Learners",
"English Learners with T 1-2", "English Learners with T 1-4",
"Hispanic", "Non-Black/Hispanic/Native Amer", "Non-ED", "Non-English Learners/T 1-2",
"Non-English Learners/T 1-4", "Non-Students with Disabilities",
"Students with Disabilities", "Super Subgroup", "White"), class = "factor")), row.names = c(2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 32L,
33L, 34L, 35L, 36L, 37L), class = "data.frame")

grid.arrange - make grid higher / extend

I would kindly like to ask for your help. I am trying to extend the graph grid below in order to fully display the three graphs. Everywhere I looked, I couldn't find any clues how to resize the actual grid, not each of the plots within them.
This is the code I am using:
p1 <- df %>%
filter(df$economic_area == "Poorer areas") %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Donations per candidate from poorer areas')
p2 <- df %>%
filter(df$economic_area == "Middle income areas") %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Donations per candidate from middle income areas')
p3 <- df %>%
filter(df$economic_area == "Upper class areas") %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Donations per candidate from upper class areas')
grid.arrange(p1, p2, p3, nrow = 3, heights = c(10, 0.65, 0.65))
Thanks a lot for your help and best regards,
Trgovec
edit:
I have tried using facets as well, but it does not work.
p <- df %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Recieved donations per candidate')
p + facet_grid(. ~ df$Median_household_income)
this is the error:
Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(536L, 1279L, 380L, :
replacement has 618217 rows, data has 25
EDIT:
here is the reproducable dataset:
> dput(test)
structure(list(cand_nm = structure(c(4L, 4L, 20L, 23L, 1L, 20L,
19L, 5L, 23L, 1L, 12L, 3L, 19L, 2L, 5L, 8L, 16L, 6L, 12L, 3L,
4L, 15L, 24L, 22L, 11L, 2L, 17L, 6L, 9L, 17L, 11L, 19L, 8L, 16L,
22L, 20L, 13L, 24L, 5L, 23L, 15L, 25L, 9L, 14L, 9L, 10L, 21L,
13L, 1L, 10L, 18L, 12L, 21L, 14L, 2L, 7L, 17L, 16L, 25L, 22L,
3L, 8L, 15L, 18L, 6L, 24L, 7L, 11L), .Label = c("Bush, Jeb",
"Carson, Benjamin S.", "Christie, Christopher J.", "Clinton, Hillary Rodham",
"Cruz, Rafael Edward 'Ted'", "Fiorina, Carly", "Gilmore, James S III",
"Graham, Lindsey O.", "Huckabee, Mike", "Jindal, Bobby", "Johnson, Gary",
"Kasich, John R.", "Lessig, Lawrence", "McMullin, Evan", "O'Malley, Martin Joseph",
"Pataki, George E.", "Paul, Rand", "Perry, James R. (Rick)",
"Rubio, Marco", "Sanders, Bernard", "Santorum, Richard J.", "Stein, Jill",
"Trump, Donald J.", "Walker, Scott", "Webb, James Henry Jr."), class = "factor"),
economic_area = structure(c(2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L,
3L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 2L,
2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 2L, 3L,
1L, 1L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 3L, 1L, 3L, 2L, 1L, 3L,
3L, 1L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 1L
), .Label = c("Poorer areas", "Middle income areas", "Upper class areas"
), class = "factor"), sum = c(125925526.85, 16410009.07,
5622760.67, 4233067.76, 2551429, 1985456.25, 1626777.31,
1154448.33, 1104488.41, 860788, 621133.75, 587637, 566709.69,
436575.16, 358550.75, 271350.07, 227328.63, 218724.23, 211130.5,
211075, 197447.84, 195618, 192773, 191260.1, 167872.43, 161952.1,
141996.17, 121297.5, 92806, 80349.55, 70725.29, 66755, 63672,
62400, 58971, 48382.4, 48291.56, 44000, 43426.5, 38671.22,
37890, 29900, 28964, 21753, 20750, 19626.42, 16655.1, 15980.75,
15021, 12550, 12250, 12150, 9900, 8434.5, 6092, 5800, 5450,
4425.66, 4050, 2936, 2700, 2700, 2700, 1750, 1000, 1000,
600, 575)), row.names = c(NA, -68L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "cand_nm", drop = TRUE, .Names = c("cand_nm",
"economic_area", "sum"), indices = list(c(4L, 9L, 48L), c(13L,
25L, 54L), c(11L, 19L, 60L), c(0L, 1L, 20L), c(7L, 14L, 38L),
c(17L, 27L, 64L), c(55L, 66L), c(15L, 32L, 61L), c(28L, 42L,
44L), c(45L, 49L), c(24L, 30L, 67L), c(10L, 18L, 51L), c(36L,
47L), c(43L, 53L), c(21L, 40L, 62L), c(16L, 33L, 57L), c(26L,
29L, 56L), c(50L, 63L), c(6L, 12L, 31L), c(2L, 5L, 35L),
c(46L, 52L), c(23L, 34L, 59L), c(3L, 8L, 39L), c(22L, 37L,
65L), c(41L, 58L)), group_sizes = c(3L, 3L, 3L, 3L, 3L, 3L,
2L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L,
3L, 3L, 2L), biggest_group_size = 3L, labels = structure(list(
cand_nm = structure(1:25, .Label = c("Bush, Jeb", "Carson, Benjamin S.",
"Christie, Christopher J.", "Clinton, Hillary Rodham", "Cruz, Rafael Edward 'Ted'",
"Fiorina, Carly", "Gilmore, James S III", "Graham, Lindsey O.",
"Huckabee, Mike", "Jindal, Bobby", "Johnson, Gary", "Kasich, John R.",
"Lessig, Lawrence", "McMullin, Evan", "O'Malley, Martin Joseph",
"Pataki, George E.", "Paul, Rand", "Perry, James R. (Rick)",
"Rubio, Marco", "Sanders, Bernard", "Santorum, Richard J.",
"Stein, Jill", "Trump, Donald J.", "Walker, Scott", "Webb, James Henry Jr."
), class = "factor")), row.names = c(NA, -25L), class = "data.frame", vars = "cand_nm", drop = TRUE, .Names = "cand_nm"))
I would basically like to combine these three graphs into one extended graph grid / page:
1)
2)
3)

Two legends for polar ggplot (with one customized)

Here is my data:
data <- structure(list(Indicator = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L), .Label = c("Causality",
"Climatechangeriskperceptions", "Currentadaptationoptions", "Fishingasalivelihoodactivity",
"Governance", "Roleofshadowstateactors"), class = "factor"),
Village = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("Andra", "lahapau", "Pelipowai", "Ponam",
"Tulu"), class = "factor"), Variables = structure(c(13L,
3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L,
9L, 4L, 7L, 13L, 3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L,
15L, 17L, 6L, 14L, 9L, 4L, 7L, 13L, 3L, 10L, 11L, 12L, 16L,
5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 9L, 4L, 7L, 13L, 3L, 10L,
11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L, 6L, 14L, 9L, 4L,
7L, 13L, 3L, 10L, 11L, 12L, 16L, 5L, 8L, 1L, 2L, 15L, 17L,
6L, 14L, 9L, 4L, 7L), .Label = c("alternativelivelihood",
"anyactorsinvolvedinsustainability", "Attributionfactors",
"discusswithelectedleaders", "Effortsdirectedtoreducerisks",
"fishercommunityinfluence", "Infrastructureeffectiveness",
"multiplicityofactors", "Occupationforchildren", "Reversibility",
"Riskasamajorconsideration", "Riskbeingaddressed", "Statusoffisheries",
"Timefishing", "Whatwasdone", "Whoisatrisk", "whowasinvolved?"
), class = "factor"), legend.var = structure(c(1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L), .Label = c("a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q"), class = "factor"),
score = c(1, 0.97, 1, 0.76, 0.794, 1, 0.71, 0.9, 0.5, 1,
1, 1, 1, 0.49, 0.72, 1, 0.7, 1, 1, 0, 0.67, 0.5, 1, 0.2,
1, 1, 0.7, 0.4, 0.5, 0.3, 0.67, 0.5, 0.7, 0.8, 1, 0, 0.46,
0.56, 0.375, 1, 0.13, 0.3, 0.5, 0.3, 0.3, 0.4, 0.6, 1, 1,
0.7, 0.8, 1, 0.86, 0.69, 0.51, 0.429, 1, 0.44, 0.3, 0.5,
0.6, 0.6, 0.7, 0.8, 0.4, 0.79, 0.8, 1, 1, 0.82, 0.85, 0.25,
0.226, 1, 0.18, 0.1, 1, 0.7, 0.3, 0.6, 0.3, 0.48, 0.16, 0.4,
0.8)), .Names = c("Indicator", "Village", "Variables", "legend.var",
"score"), class = "data.frame", row.names = c(NA, -85L))
I have made polar plots as follows:
library(ggplot2)
ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') +
geom_bar(width = 1, alpha=0.5, stat="identity") +
scale_y_continuous() +
coord_polar() +
theme( axis.ticks = element_blank()) +
facet_wrap(~Village, nrow=2, ncol=3) +
guides(colour = guide_legend(title.hjust = 0.5))
As you can see in the data, column legend.var is grouped by the column indicator. I would like to insert a table or a second legend which associates each of the indicator with the legend.var and variables column. Ideally if this is inserted as a second legend, the unique items comprised in legend.var column would have the same fill color as the corresponding indicator. The fill legend is based on the column indicator. The inserted table/extra legend would comprise columns legend.var with a unique alphabet and column variables with the meaning of the respective alphabet. These can then have the same fill color as the corresponding indicator. I hope this is clear.
Here is a solution using a gtable:
library(ggplot2)
p <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') +
geom_bar(width = 1, alpha=0.5, stat="identity") +
scale_y_continuous() +
coord_polar() +
theme( axis.ticks = element_blank()) +
facet_wrap(~Village, nrow=2, ncol=3) +
guides(colour = guide_legend(title.hjust = 0.5)) +
theme(legend.position=c(0.85,0.25))
#create table
library(gridExtra)
tab <- tableGrob(unique(data[, c("legend.var", "Variables")]),
show.rownames=FALSE, gpar.coretext=gpar(fontsize=10),
gpar.coltext=gpar(fontsize=10, fontface='bold'),
gpar.corefill = gpar(fill = "grey90", col = "white"),
gpar.colfill = gpar(fill = "grey80", col = "white"))
#arrange grobs
library(gtable)
a <- gtable(unit(c(0.7, 0.3) ,c("npc")), unit(1, "npc"))
a <- gtable_add_grob(a, ggplotGrob(p),1,1)
a <- gtable_add_grob(a, tab,1,2)
#plot
grid.draw(a)
For a start, you may try something like this. You need to adjust arrangement and layout according to your own preferences.
library(ggplot2)
library(gridExtra)
gg <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') +
geom_bar(width = 1, alpha=0.5, stat="identity") +
coord_polar() +
theme(axis.ticks = element_blank()) +
facet_wrap(~Village, nrow=2, ncol = 3)
# create a table that translates legend.var to Variables
tbl <- unique(data[ , c("legend.var", "Variables")])
# create a table grob
tt <- tableGrob(d = tbl,
col.just = "left",
gpar.coretext = gpar(col = "black", cex = 0.5),
gpar.coltext = gpar(col = "black", cex = 0.5, fontface = "bold"),
gpar.rowtext = gpar(col = "black", cex = 0.5, fontface = "italic"))
# arrange plot and table grob
grid.arrange(gg, tt, ncol = 2)
update with a quick and dirty ggplot-only alternative
# create labels
labs <- with(tbl, paste(legend.var, Variables))
gg <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator, col = Variables)) +
geom_bar(width = 1, alpha = 0.5, stat = "identity") +
coord_polar() +
theme(axis.ticks = element_blank()) +
facet_wrap(~ Village, nrow = 2, ncol = 3) +
scale_color_grey(labels = labs, start = 0.8, end = 0.9)
gg

Resources