Geom_txt does not display correctly in animation - r

Sorry for my English.
I'm creating a feature to display the NBA season moving average. I'm doing an animation, part of which is displaying the current rating. I do this with geom_text. The problem is that instead of displaying a single value, you get a jumble.
Code:
library(httr)
library(jsonlite)
library(tidyverse)
##Getting data via NBA API.
##Required link
adv_box_team <- "https://stats.nba.com/stats/teamgamelogs?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlusMinus=N&Rank=N&Season=2018-19&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&VsConference=&VsDivision="
##Adding headers
request_headers <- c(
"accept-encoding" = "gzip, deflate, sdch",
"accept-language" = "en-US,en;q=0.8",
"cache-control" = "no-cache",
"connection" = "keep-alive",
"host" = "stats.nba.com",
"pragma" = "no-cache",
"upgrade-insecure-requests" = "1",
"user-agent" = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9"
)
#Getting a response
request <- GET(adv_box_team, add_headers(request_headers))
#Convert to js.file to list
boxscore_data <- fromJSON(content(request, as = "text"))
#Convert to tibble data and assigning column names
table <- tbl_df(data.frame(boxscore_data$resultSets$rowSet[[1]], stringsAsFactors = FALSE))
names(table) <- toupper(boxscore_data$resultSets$headers[[1]])
library(tidyverse)
library(lubridate)
library(zoo)
library(ggthemes)
library(gganimate)
library(rlang)
library(data.table)
##Cleaning data
rating <- table %>%
select(TEAM_ID,
TEAM_ABBREVIATION,
TEAM_NAME,
GAME_ID,
GAME_DATE,
MATCHUP,
WL,
E_OFF_RATING,
E_DEF_RATING,
E_NET_RATING)
rating1 <- rating %>%
rename_at(vars(starts_with("E_")),
funs(str_c(str_sub(., start = 3, end = 3),
str_sub(., start = 7, end = 7),
str_sub(., start = 9, end = 9),
str_sub(., start = 12, end = 12))))
rolling_offnet_rating_nba <- function(table, variable, name, col1 = col1, col2 = col2){
quo_rating <- enquo(variable)
quo_col1 <- enquo(col1)
quo_col2 <- enquo(col2)
test1 <- rating1 %>%
mutate(GAME_DATE = as.Date(ymd_hms(GAME_DATE))) %>%
mutate_at(vars(ORTG:NRTG), list(~as.numeric))
team <- test1 %>%
filter(TEAM_ABBREVIATION == name) %>%
mutate(RATING = rollmeanr(!! quo_rating, k = 10, fill= NA)) %>%
na.omit(test1)
league <- test1 %>%
group_by(TEAM_NAME) %>%
summarise(ORTG = mean(ORTG),
DRTG = mean(DRTG),
NTRG = mean(NRTG))
average <- league %>%
mutate(average = mean(!! quo_rating)) %>%
select(average) %>%
unique() %>%
.$average
top10 <- league %>%
arrange(desc(!! quo_rating)) %>%
select(!! quo_rating) %>%
slice(10)
top10 <- top10[[1]]
bottom10 <- league %>%
arrange(desc(!! quo_rating)) %>%
select(!! quo_rating) %>%
slice(21)
bottom10 <- bottom10[[1]]
data <- team %>%
select(GAME_DATE) %>%
unique() %>%
arrange(GAME_DATE)
data <- data[[1,1]]
table_color <- data.table(TEAM_ID = c(1610612737, 1610612738, 1610612751, 1610612766, 1610612741, 1610612739, 1610612742,
1610612743, 1610612765, 1610612744, 1610612745, 1610612754, 1610612746, 1610612747,
1610612763, 1610612748, 1610612749, 1610612750, 1610612740, 1610612752, 1610612760,
1610612753, 1610612755, 1610612756, 1610612757, 1610612758, 1610612759, 1610612761,
1610612762, 1610612764),
TEAM_NAME = c("Atlanta Hawks", "Boston Celtics", "Brooklyn Nets",
"Charlotte Hornets", "Chicago Bulls", "Cleveland Cavaliers",
"Dallas Mavericks", "Denver Nuggets", "Detroit Pistons",
"Golden State Warriors", "Houston Rockets", "Indiana Pacers",
"LA Clippers", "Los Angeles Lakers", "Memphis Grizzlies",
"Miami Heat", "Milwaukee Bucks", "Minnesota Timberwolves",
"New Orleans Pelicans", "New York Knicks", "Oklahoma City Thunder",
"Orlando Magic", "Philadelphia 76ers", "Phoenix Suns",
"Portland Trail Blazers", "Sacramento Kings", "San Antonio Spurs",
"Toronto Raptors", "Utah Jazz", "Washington Wizards"),
TEAM_ABBREVIATION = c("ATL", "BOS", "BKN", "CHA", "CHI", "CLE", "DAL", "DEN", "DET", "GSW", "HOU", "IND", "LAC", "LAL",
"MEM", "MIA", "MIL", "MIN", "NOP", "NYK", "OKC", "ORL", "PHI", "PHX", "POR", "SAC", "SAS", "TOR",
"UTA", "WAS"),
col1 = c("#E03A3E", "#007A33", "#000000", "#1D1160", "#CE1141", "#6F263D", "#00538C", "#0E2240",
"#C8102E", "#006BB6", "#CE1141", "#002D62", "#C8102E", "#552583", "#5D76A9", "#98002E",
"#00471B", "#0C2340", "#0C2340", "#006BB6", "#007AC1", "#0077C0", "#006BB6", "#1D1160",
"#E03A3E", "#5A2D81", "#C4CED4", "#CE1141", "#002B5C", "#002B5C"),
name_col1 = c("HAWKS RED", "CELTICS GREEN", "BLACK",
"HORNETS PURPLE", "BULLS RED", "CAVALIERS WINE",
"ROYAL BLUE", "MIDNIGHT BLUE", "RED",
"WARRIORS ROYAL BLUE", "RED", "PACERS BLUE",
"RED", "LAKERS PURPLE", "BLUE",
"RED", "GOOD LAND GREEN", "MIDNIGHT BLUE",
"PELICANS NAVY", "KNICKS BLUE", "THUNDER BLUE",
"MAGIC BLUE", "BLUE", "PURPLE",
"RED", "PURPLE", "SILVER",
"RED", "NAVY", "NAVY BLUE"),
col2 = c("#C1D32F", "#BA9653", "#FFFFFF", "#00788C", "#000000", "#041E42", "#002B5E", "#FEC524",
"#006BB6", "#FDB927", "#000000", "#FDBB30", "#1D428A", "#FDB927", "#12173F", "#F9A01B",
"#EEE1C6", "#236192", "#C8102E", "#F58426", "#EF3B24", "#C4CED4", "#ED174C", "#E56020",
"#000000", "#63727A", "#000000", "#000000", "#00471B", "#E31837"),
name_col2 = c("VOLT GREEN", "CELTICS GOLD", "WHITE", "TEAL",
"BLACK", "CAVALIERS NAVY", "NAVY BLUE", "SUNSHINE YELLOW",
"ROYAL", "GOLDEN YELLOW", "BLACK", "YELLOW",
"BLUE", "GOLD", "NAVY", "YELLOW",
"CREAM CITY CREAM", "LAKE BLUE", "PELICANS RED", "KNICKS ORANGE",
"SUNSET", "SILVER", "RED", "ORANGE",
"BLACK", "GRAY", "BLACK", "BLACK",
"GREEN", "RED"),
col3 = c("#26282A", "#963821", NA, "#A1A1A4", NA, "#FFB81C", "#B8C4CA", "#8B2131",
"#BEC0C2", "#26282A", "#C4CED4", "#BEC0C2", "#BEC0C2", "#000000", "#F5B112", "#000000",
"#0077C0", "#9EA2A2", "#85714D", "#BEC0C2", "#002D62", "#000000", "#002B5C", "#000000",
NA, "#000000", NA, "#A1A1A4", "#F9A01B", "#C4CED4"),
name_col3 = c("HAWKS CHARCOAL", "CELTICS BROWN", NA, "GRAY",
NA, "CAVALIERS NAVY", "SILVER", "FLATIRONS RED",
"GRAY", "SLATE", "SILVER", "SILVER",
"SILVER", "BLACK", "YELLOW", "BLACK",
"GREAT LAKES BLUE", "MOONLIGHT GREY", "PELICANS GOLD", "KNICKS SILVER",
"BLUE", "BLACK", "NAVY", "BLACK",
NA, "BLACK", NA, "SILVER",
"YELLOW", "SILVER" ),
col4 = c( NA, "#E59E6D", NA, NA, NA, "#000000", "#000000", "#1D428A",
"#002D62", NA, NA, NA, "#000000", NA, "#707271", NA,
"#000000", "#78BE20", NA, "#000000", "#FDBB30", NA, "#C4CED4", "#63727A",
NA, NA, NA, "#B4975A", NA, NA ),
name_col4 = c( NA, "CELTICS BEIGE", NA, NA,
NA, "CAVALIERS BLACK", "BLACK", "SKYLINE BLUE",
"NAVY", NA, NA, NA,
"BLACK", NA, "GRAY", NA,
"BLACK", "AURORA GREEN", NA, "KNICKS BLACK",
"YELLOW", NA, "SILVER", "GRAY",
NA, NA, NA, "GOLD",
NA, NA ),
col5 = c(NA, "#000000", NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, "#F9AD1B",
NA, NA, NA, NA, NA, NA ),
name_col5 = c(NA, "CELTICS BLACK", NA, NA, NA,
NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA,
NA, NA, NA, "YELLOW", NA,
NA, NA, NA, NA, NA ),
col6 = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, "#B95915",
NA, NA, NA, NA, NA, NA ),
name_col6 = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, "DARK ORANGE",
NA, NA, NA, NA, NA, NA ),
col7 = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, "#BEC0C2",
NA, NA, NA, NA, NA, NA ),
name_col7 = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, "LIGHT GRAY",
NA, NA, NA, NA, NA, NA))
color1 <- table_color %>%
filter(TEAM_ABBREVIATION == name) %>%
select(!! quo_col1)
color1 <- color1[[1]]
color2 <- table_color %>%
filter(TEAM_ABBREVIATION == name) %>%
select(!! quo_col2)
color2 <- color2[[1]]
name1 <- paste0("name_", quo_name(quo_col1))
name2 <- paste0("name_", quo_name(quo_col2))
name_color1 <- table_color %>%
filter(TEAM_ABBREVIATION == name) %>%
select(name1)
name_color1 <- name_color1[[1]]
name_color2 <- table_color %>%
filter(TEAM_ABBREVIATION == name) %>%
select(name2)
name_color2 <- name_color2[[1]]
max <- team %>%
filter(RATING == max(RATING)) %>%
select(RATING)
max <- max[[1]]
Sys.setlocale("LC_ALL", "C")
gg <- ggplot(team, aes(GAME_DATE, RATING)) +
geom_hline(yintercept = c(top10, bottom10), col = c("red", "blue")) +
annotate(geom = "text", x = as.Date(data) + 2, y = top10 - 0.2,
label = "TOP 10", col = "red") +
annotate(geom = "text", x = as.Date(data) + 2, y = bottom10 + 0.2,
label = "BOTTOM 10", col = "blue") +
geom_line(size = 2, col = if_else(team$RATING > average, color1, color2)) +
theme_tufte() +
labs(title = paste0(team$TEAM_NAME, " 10-Game Rolling Luck-Adjusted ", quo_name(quo_rating)),
subtitle = paste0(paste0(name_color1, " - above average ", quo_name(quo_rating)),
"\n", paste0(name_color2, " - below average ",quo_name(quo_rating))),
caption = "Source: BBall Index Data & Tools\nTelegram: #NBAatlantic, twitter: #vshufinskiy")
theme(plot.title = element_text(size = 12, hjust = 0.5),
plot.caption = element_text(size = 10),
plot.subtitle = element_text(size = 9))
ggsave(paste0(unique(team$TEAM_NAME), quo_name(quo_rating), ".jpeg"), gg, width = 8, units = "in")
anim <- gg +
theme(plot.title = element_text(hjust = 0.5, size = 25),
plot.subtitle = element_text(size = 15),
plot.caption = element_text(size = 15),
axis.text = element_text(size = 15),
axis.title = element_text(size = 18)) +
geom_text(aes(x = as.Date(data), y = max + 0.5),
label = paste0(quo_name(quo_rating)," ", round(team$RATING, digits = 1)), size = 6,
col = if_else(team$RATING > average, color1, color2)) +
transition_reveal(GAME_DATE) +
labs(title = paste0(team$TEAM_NAME, " 10-Game Rolling Luck-Adjusted ", quo_name(quo_rating)),
subtitle = paste0(paste0(name_color1, " - above average ",quo_name(quo_rating)),
"\n", paste0(name_color2, " - below average ",quo_name(quo_rating)),
"\n", "Date: {frame_along}"),
caption = paste0("Source: BBall Index Data & Tools\nTelegram: #NBAatlantic, twitter: #vshufinskiy"))
animate(anim, fps = 5, duration = 5, width = 1280, height = 720,
renderer = gifski_renderer(paste0(unique(team$TEAM_NAME), quo_name(quo_rating), ".gif")))
}
rolling_offnet_rating_nba(rating1, ORTG, "GSW")
Result: https://c.radikal.ru/c40/1907/c8/37e210e3f31b.gif

Related

GG plot legend stacking all shapes / deleting one item from legend

There is a problem with my ggplot, I want to have different shapes for certain values. But in the legend all thing stack on top of each other and therefore the legend is not clear anymore. Moreover, I would like to delete Japan from the legend or find another way to make it red and shape=17. Maybe add to the filter Japan=='FALSE'? I tried but did not succeed...
Here is sample of my data:
structure(list(Country = c("Albania", "Aruba", "Austria", "Barbados",
"Bosnia and Herzegovina", "Canada", "China, Hong Kong SAR", "China, Macao SAR",
"Croatia", "CuraƧao", "Denmark", "Finland", "France", "Germany",
"Iceland", "Italy", "Japan", "Latvia", "Lithuania", "Malta",
"Mauritius", "Montenegro", "Netherlands", "New Zealand", "Poland",
"Portugal", "Republic of Korea", "Serbia", "Singapore", "Slovenia",
"Sri Lanka", "Taiwan", "Thailand", "Trinidad and Tobago", "United States of America"
), `Dependency Ratio 1990` = c(0.371731839842905, 0.42945960478559,
0.698167620530499, 0.444513116903726, 0.511357742868368, 0.519783119456753,
0.444426949479237, 0.30306654331295, 0.723691486939267, 0.424414908111054,
0.68769508504734, 0.641530173960242, 0.690189226564259, 0.755969184286434,
0.520917100019657, 0.763735128335739, 0.692461922514607, 0.728970209495916,
0.655093765838824, 0.556158238426314, 0.308439455191019, 0.551893405455789,
0.582543266573117, 0.548269437314668, 0.592240027149362, 0.744368260326749,
0.33818760118961, 0.653157768845158, 0.294237762460344, 0.611402526341597,
0.354595845574429, 0.391092962761626, 0.331304119150256, 0.35111793456609,
0.562804979721953), `Average Age 1990` = c(40.3688042387203,
42.5004114258846, 46.6904752788518, 42.5683625031078, 42.4530074518545,
44.2409448871874, 42.0677766503007, 39.7173235436725, 46.2329924328207,
42.2501753565583, 47.1375106133558, 46.3380103826365, 46.6915593676301,
46.9202073747455, 44.129974503284, 47.1071528898825, 46.6077408054755,
46.4664135824761, 45.460688263743, 44.9450928096016, 39.2332051727974,
43.66848, 45.1863467813393, 44.5466909246095, 44.9318462263063,
46.8407998745322, 39.6873706785703, 45.3128111624097, 39.2982502106955,
45.1205082490539, 40.2124158913374, 40.9051762916043, 39.4534335710941,
40.4173693037492, 45.0904477728946), ...4 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
`Dependency Ratio 2000` = c(0.457221087782508, 0.45940989018547,
0.742565467519652, 0.541204030550029, 0.597208883500012,
0.622668782217446, 0.473107405821069, 0.344264744501091,
0.780230513979289, 0.633789601501269, 0.790840886898238,
0.816743961984905, 0.766496601277572, 0.821136017787255,
0.572251283384235, 0.849390138872188, 0.927739183233871,
0.791488299481733, 0.704545225683664, 0.702609326498817,
0.35385418751795, 0.612564625368555, 0.69550083971213, 0.617845149047375,
0.611138887992547, 0.758176723785889, 0.399735388267277,
0.715467873467691, 0.383896159972764, 0.671137540638121,
0.407831309113246, 0.419443507121452, 0.374126385687095,
0.409593048372564, 0.615930392620661), `Average Age 2000` = c(42.9309383891972,
43.8674007980144, 47.739334648896, 45.3560289004102, 45.2990249348384,
46.3058678455289, 44.3575197674921, 42.5567755821042, 47.8706196243093,
46.6926342578517, 47.9056748231027, 48.2912968951969, 48.1201704908476,
48.7598382100637, 45.3475147626354, 48.7908038019529, 48.7739160208226,
47.837540150878, 46.9150297452015, 46.5384376276976, 41.1677637838199,
45.6830176554619, 46.9484767952653, 46.0162750047118, 45.6870193241911,
47.5083105450284, 41.9472090972845, 46.7580169116961, 42.3830413567395,
46.6274879755993, 41.6098644987726, 42.77485916275, 41.5907978667698,
41.9719792296039, 46.2781534087236), ...7 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA), `Dependency Ratio 2015` = c(0.769855001031037, 0.896573728647162,
0.952116351996821, 0.847635802309437, 0.907658891504387,
0.913339561634508, 0.877436210055064, 0.597279210234922,
1.04631340359464, 1.01174318826707, 0.977891738355926, 1.08472890326446,
1.01372962185931, 1.12997699958302, 0.77882885539859, 1.08347132170333,
1.20679093156161, 0.990147667477283, 0.994357961860711, 0.950194923573131,
0.666562280517562, 0.828817278052088, 0.989460150334804,
0.831814166715077, 0.847326002560978, 1.03326726133893, 0.749863481391909,
0.917215066264046, 0.671774028057953, 0.971441198307662,
0.611354032233621, 0.748057645284422, 0.665957813686028,
0.602213503073687, 0.855245238291093), `Average Age 2015` = c(46.3902100558352,
47.9818955923079, 49.878498965043, 48.4578763127188, 48.3870694416244,
48.7063314226308, 48.5131007402609, 44.1281261495054, 50.522561636728,
49.6455293947711, 49.9810732770387, 50.7119476819108, 50.6805196046482,
51.5236122201751, 47.3079677856577, 51.8838669025279, 53.2344169277342,
50.0674941000466, 49.9057070057583, 49.1748722211516, 45.3413745873924,
47.6617051653597, 49.9107746561504, 48.0022465682781, 48.1684244717051,
51.1526322354916, 47.160655712273, 49.0269050604693, 45.5481140676913,
50.0264456515826, 44.8882173741791, 47.0138589294768, 46.131374630996,
44.5204789350954, 48.0998439723386), `rgdpe 1990` = c(12005.7568359375,
2575.25561523438, 208007.234375, 4099.8515625, 6946.330078125,
915724.6875, 151044.28125, 9127.78125, 64448.71484375, NA,
144018.203125, 136194.359375, 1581529.625, 2204488.5, 8510.248046875,
1560881.5, 3552613.25, 44957.03515625, 53623.6875, 5354.54541015625,
11257.8095703125, 6702.1552734375, 427072.25, 76859.65625,
335254.875, 157535.140625, 565140.75, 113435.8046875, 64860.5703125,
42714.70703125, 55257.37109375, 430917.25, 308367.4375, 15085.6611328125,
9847675), `pop 1990` = c(3.286073, 0.062149, 7.723949, 0.260936,
4.463423, 27.541319, 5.727938, 0.343808, 4.776374, NA, 5.141115,
4.996222, 58.235697, 79.053984, 0.255043, 57.048236, 124.50524,
2.664439, 3.696035, 0.362015, 1.055868, 0.615002, 14.965448,
3.398172, 37.960193, 9.895364, 42.918419, 9.517675, 3.012966,
2.006405, 17.325773, 20.278946, 56.558186, 1.221116, 252.120309
), `emp 1990` = c(1.32407820224762, NA, 3.56034135818481,
0.105200000107288, 1.68987882137299, 13.2902002334595, 2.73075985908508,
0.16329999268055, 2.17813229560852, NA, 2.63417220115662,
2.47324681282043, 23.6595039367676, 39.5477294921875, 0.138074412941933,
22.8031978607178, 65.1040191650391, 1.25425291061401, 1.70560574531555,
0.132750615477562, 0.403737008571625, 0.174824863672256,
6.80782461166382, 1.52131986618042, 15.0829668045044, 4.46721506118774,
18.2060832977295, 4.61394643783569, 1.52955627441406, 1.1292530298233,
5.04270553588867, 8.64918994903564, 28.7045097351074, 0.374099999666214,
123.046020507812), ...5 = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), `rgdpe 2000` = c(15180.880859375,
4031.13427734375, 314579.6875, 6460.9755859375, 21482.595703125,
1276875.5, 256729.140625, 13199.31640625, 58379.2734375,
NA, 203091.515625, 186055.46875, 2135621.25, 3030253, 11653.5791015625,
2081385.625, 4696670.5, 26473.306640625, 42346.78125, 9706.8212890625,
18593.318359375, 4850.50634765625, 691869.625, 114914.1171875,
563679.1875, 259576.953125, 1150272, 60002.0703125, 166273.53125,
48606.53125, 90442.875, 789527.25, 504829.28125, 18303.63671875,
14110581), `pop 2000` = c(3.129243, 0.090853, 8.069276, 0.271515,
3.751176, 30.588383, 6.606327, 0.427782, 4.428075, NA, 5.341194,
5.187954, 60.874357, 81.400882, 0.280435, 56.692178, 127.524174,
2.384164, 3.501839, 0.393645, 1.185145, 0.613559, 15.926188,
3.858999, 38.556693, 10.297112, 47.379241, 7.516346, 4.028871,
1.987717, 18.777601, 22.18453, 62.952642, 1.267153, 281.710909
), `emp 2000` = c(0.962967455387115, 0.0419000014662743,
3.7599310874939, 0.12899999320507, 0.643303751945496, 14.952766418457,
3.20262169837952, 0.195299997925758, 1.67029082775116, NA,
2.75595617294312, 2.30501818656921, 25.6252250671387, 39.6031150817871,
0.1570855230093, 22.91796875, 65.9155044555664, 0.930018603801727,
1.40124833583832, 0.146938025951385, 0.464872002601624, 0.176752656698227,
8.20334815979004, 1.81842231750488, 14.4786930084229, 5.076171875,
21.4411239624023, 3.0847954750061, 2.08465480804443, 0.917375922203064,
6.30462980270386, 9.59665679931641, 31.47385597229, 0.503100037574768,
138.636108398438), ...9 = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), `rgdpe 2015` = c(32037.935546875,
3959.59252929688, 448794.71875, 4856.21044921875, 40599.22265625,
1659691.75, 411350.65625, 62493.578125, 99181.7265625, 4041.00463867188,
278112.53125, 237412.921875, 2772463.25, 3915258.25, 16865.345703125,
2296760.75, 5094436, 51517.390625, 87529.2890625, 17455.76171875,
25619.560546875, 10165.248046875, 872643.75, 174613.65625,
1069768.375, 314019.625, 1928056.875, 108470.875, 451476.4375,
68875.1796875, 242116.15625, 1125999, 1108115.875, 38140.46484375,
18905122), `pop 2015` = c(2.890513, 0.104341, 8.67866, 0.285324,
3.429361, 36.026676, 7.185996, 0.602085, 4.232874, 0.159847,
5.688695, 5.481122, 66.596315, 81.787411, 0.330243, 60.578494,
127.985133, 1.997674, 2.93188, 0.433559, 1.259456, 0.626956,
16.938499, 4.614532, 38.034079, 10.368351, 50.823093, 7.095383,
5.592152, 2.071199, 20.908027, 23.462914, 68.714511, 1.370328,
320.87831), `emp 2015` = c(0.926395297050476, 0.0467174984514713,
4.27823972702026, 0.128199994564056, 0.616872131824493, 18.3558368682861,
3.77715754508972, 0.396699994802475, 1.69973313808441, 0.0617999993264675,
2.83158588409424, 2.52453279495239, 27.3850765228271, 42.5355796813965,
0.181162342429161, 24.4446144104004, 66.9830322265625, 0.897909104824066,
1.35488307476044, 0.191138163208961, 0.563370883464813, 0.221699982881546,
8.80725860595703, 2.36527323722839, 15.8249969482422, 4.60829973220825,
26.079252243042, 2.56693267822266, 3.65548992156982, 0.950674414634705,
7.83100032806396, 11.1978015899658, 37.9529876708984, 0.623300015926361,
150.248474121094), GDP_per_capita_1990 = c(3653.52712369369,
41436.7989064084, 26930.1667288326, 15712.0963090566, 1556.27868524337,
33249.1224367286, 26369.74793547, 26549.0659030622, 13493.2303968973,
NA, 28013.0289100711, 27259.4691298745, 27157.3915394195,
27885.8621470614, 33367.895009371, 27360.7320654051, 28533.8452421762,
16872.9834521451, 14508.4360672991, 14790.949021881, 10662.1372845019,
10897.7780128154, 28537.2178634412, 22617.9417198423, 8831.74843183753,
15920.0955745539, 13167.790500391, 11918.4364550691, 21527.1497628915,
21289.1749329024, 3189.31634933402, 21249.4894951641, 5452.2158383934,
12353.995142814, 39059.4277750151), GDP_per_capita_2015 = c(11083.8233721402,
37948.5775418759, 51712.4439429589, 17019.9858729681, 11838.7135843237,
46068.41191788, 57243.3739526156, 103795.274961177, 23431.2966940429,
25280.4534252872, 48888.6346077615, 43314.6574506096, 41630.8807777127,
47871.1601471283, 51069.5024667442, 37913.7974278463, 39804.9045274657,
25788.6875561278, 29854.3218216639, 40261.5600616064, 20341.7670382094,
16213.6546214966, 51518.3635810942, 37839.9491541071, 28126.5749855544,
30286.3613509998, 37936.6300079375, 15287.5292285138, 80733.9352542635,
33253.7721809927, 11580.0575659291, 47990.5863355251, 16126.3735835943,
27833.0916713006, 58916.7962147395), change_log_GDP_per_cap_1990_2015 = c(1.10979365523243,
-0.0879373277889428, 0.652451349776046, 0.0799574118764497,
2.02907746023446, 0.326099133412088, 0.775094455358737, 1.36342588458029,
0.551884486717116, NA, 0.556875228192604, 0.46309013522626,
0.427192960710386, 0.540393411165555, 0.425593287986558,
0.32620624489414, 0.332899198982464, 0.424222198470275, 0.721599336011078,
1.00138172800831, 0.645977367508866, 0.39729484866726, 0.590729193215088,
0.514621927745218, 1.15836185302803, 0.643115306926695, 1.05814340225474,
0.248950929776939, 1.32184407992377, 0.44596948530371, 1.28947786000817,
0.814672002339478, 1.08443394063506, 0.812246156576203, 0.411041939497887
), change_dependency_ratio_1990_2015 = c(0.398123161188132,
0.467114123861573, 0.253948731466323, 0.403122685405711,
0.396301148636019, 0.393556442177755, 0.433009260575827,
0.294212666921972, 0.322621916655375, 0.587328280156014,
0.290196653308586, 0.44319872930422, 0.323540395295048, 0.374007815296586,
0.257911755378933, 0.319736193367588, 0.514329009047002,
0.261177457981367, 0.339264196021887, 0.394036685146817,
0.358122825326543, 0.2769238725963, 0.406916883761686, 0.283544729400408,
0.255085975411616, 0.288899001012185, 0.411675880202299,
0.264057297418888, 0.377536265597609, 0.360038671966065,
0.256758186659192, 0.356964682522796, 0.334653694535773,
0.251095568507597, 0.29244025856914)), class = "data.frame", row.names = c(NA,
-35L))
And this is my code:
#Adding OECD
#Exclude japan for fig
OECD <- c("Dem. People's Republic of Korea",'Mexico','Chile',
'New Zealand','Czechia','Hungary','Slovakia','Denmark','Estonia',
'Finland','Iceland','Ireland','Latvia','Lithuania','Norway',
'Sweden','United Kingdom','Greece','Italy','Portugal','Slovenia',
'Spain','Austria','Belgium','France','Germany','Luxembourg',
'Netherlands','Switzerland','Australia','Canada','United States of America',
'Poland','Turkey','Israel')
#Figure 2
library(tidyverse)
df %>%
mutate(OECD = factor(Country %in% OECD, labels = c("NonOECD","OECD"))) %>% mutate(Japan = factor(Country == 'Japan' , labels=c('FALSE','TRUE')))-> newdata
ggplot() +
geom_point(data = filter(newdata, OECD == 'NonOECD'),aes(x = change_dependency_ratio_1990_2015, y = change_log_GDP_per_cap_1990_2015, colour='NonOECD'),shape = 16, size=3) +
geom_point(data = filter(newdata, Japan == 'TRUE'),aes(x = change_dependency_ratio_1990_2015, y = change_log_GDP_per_cap_1990_2015,colour='Japan'),shape = 17, size=4) +
geom_point(data = filter(newdata, OECD == 'OECD'),aes(x = change_dependency_ratio_1990_2015, y = change_log_GDP_per_cap_1990_2015, colour='OECD' ),shape = 18, size=4) +
scale_color_manual(values = c(NonOECD = "cyan4", OECD = "orange",Japan='red'),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries")) + geom_abline(size=1, col='grey')+
theme_classic()+ theme( panel.grid.major.y =element_line(color='grey', size=0.7),legend.title = element_blank(),
panel.grid.minor.y =element_blank(),
legend.background = element_blank(), legend.box.background = element_rect(colour = "black"),
legend.spacing.y = unit(0, "mm"),legend.direction = 'horizontal',
legend.position = "bottom",aspect.ratio = 0.7, axis.text = element_text(colour = 1, size = 13),)
Example of what I got:
And what it should look like:
Thanking you in advance!!
The code below produces a plot equivalent to the expected output.
The two main differences are:
There is no data for "Japan" in the question so I have substituted "Portugal" (my country) for it;
There is no GDP data, logged or not so I have created a new column with random uniform numbers, runif.
The plot is in fact simple, to create the factor OECD start by creating a logical vector, then use an ifelse to assign an integer value to the special country, in this case "Portugal" and add 2 to the other logical vector's elements, giving FALSE/TRUE + 2 == 0/1 + 2.
In order not to mix the plot with the theme, I have also created a custom theme, with code at the end.
library(tidyverse)
set.seed(2021)
df %>%
mutate(OECD = Location %in% OECD,
OECD = ifelse(Location == "Portugal", 1L, OECD + 2L),
OECD = factor(OECD, labels = c("Portugal", "NonOECD","OECD"))) %>%
mutate(GDP = runif(n(), -2, 2)) %>%
ggplot(aes(x = `Dependency Ratio`, y = GDP, color = OECD, shape = OECD, size = OECD)) +
geom_point() +
scale_color_manual(
values = c(NonOECD = "cyan4", OECD = "orange", Portugal = 'red'),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
scale_shape_manual(
values = c(NonOECD = 16, OECD = 18, Portugal = 17),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
scale_size_manual(
values = c(NonOECD = 4, OECD = 3, Portugal = 4),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
geom_abline(size = 1, col = 'grey') +
theme_custom_Cas()
In order to remove the special country from the legend, subset the data. The code below will output the %>% pipe to a new data set and used it in the plot.
set.seed(2021)
df %>%
mutate(OECD = Location %in% OECD,
OECD = ifelse(Location == "Portugal", 1L, OECD + 2L),
OECD = factor(OECD, labels = c("Portugal", "NonOECD","OECD"))) %>%
mutate(GDP = runif(n(), -2, 2)) -> newdata
ggplot(newdata, aes(x = `Dependency Ratio`, y = GDP, color = OECD, shape = OECD, size = OECD)) +
geom_point(data = subset(newdata, OECD != "Portugal")) +
# In the special country's layer the color, shape and size must be
# outside aes() and show.legend = FALSE
geom_point(
data = subset(newdata, OECD == "Portugal"),
color = "red", shape = 17, size = 4,
show.legend = FALSE
) +
scale_color_manual(
values = c(NonOECD = "cyan4", OECD = "orange", Portugal = 'red'),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
scale_shape_manual(
values = c(NonOECD = 16, OECD = 18, Portugal = 17),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
scale_size_manual(
values = c(NonOECD = 4, OECD = 3, Portugal = 4),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
geom_abline(size = 1, col = 'grey') +
theme_custom_Cas()
In order to have a fill color, the points shapes must be changed. See in help("points") the rightmost points, filled in grey. Those shapes allow for a border (ggplot aesthetic color) and a fill color (ggplot aesthetic fill).
ggplot(newdata, aes(x = `Dependency Ratio`, y = GDP, fill = OECD, shape = OECD, size = OECD)) +
geom_point(data = subset(newdata, OECD != "Portugal")) +
# In the special country's layer the color, shape and size must be
# outside aes() and show.legend = FALSE
geom_point(
data = subset(newdata, OECD == "Portugal"),
fill = "red", shape = 24, size = 4,
show.legend = FALSE
) +
scale_fill_manual(
values = c(NonOECD = "cyan4", OECD = "orange", Portugal = 'red'),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
scale_shape_manual(
values = c(NonOECD = 21, OECD = 23, Portugal = 24),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
scale_size_manual(
values = c(NonOECD = 4, OECD = 3, Portugal = 4),
labels = c(NonOECD = "All Countries except OECD", OECD = "OECD countries", Portugal = "Portugal")
) +
geom_abline(size = 1, col = 'grey') +
theme_custom_Cas()
Custom theme code.
theme_custom_Cas <- function(){
theme_classic() %+replace% #replace elements we want to change
theme(
panel.grid.major.y = element_line(color = 'grey', size = 0.7),
legend.title = element_blank(),
panel.grid.minor.y = element_blank(),
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
legend.spacing.y = unit(0, "mm"),
legend.direction = 'horizontal',
legend.position = "bottom",
aspect.ratio = 0.7,
axis.text = element_text(colour = 1, size = 13)
)
}

ggplot2 | How to customize the order of string values in the legend?

In continuation of my earlier question, I am facing issues w.r.t. to ordering the legends. The initially posted question had ordinal (ordered) values and hence worked perfectly. In real-time, the data rendered in the legend is being ordered alphabetically.
library(ggplot2)
library(tidyverse)
library(reshape2)
#Creating a dataframe with use-case specific variables.
df = data.frame(
Year = 2006:2025,
Survey = c(40.5, 39.0, NA, NA, NA, NA, 29.9, NA, NA, NA, 21.6,
NA, NA, NA, NA, NA, NA, NA, NA, NA),
Projected1 = c(NA, NA, NA, NA, NA, NA, 29.9, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 14.9),
WhatIf= c(NA, NA, NA, NA, NA, NA, 29.9, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 13.0),
Projected2 = c(NA, NA, NA, NA, NA, NA, 29.9, 27.6, 25.4, 23.4, 21.6,
19.9, 18.4, 16.9, 15.6, 14.4, 13.3, NA, 12.2, 11.3)
)
#Transforming data
df <- melt(df,id.vars = "Year")
ggplot(data = NULL, aes(x=factor(Year), y=value, group=variable)) +
geom_line(data = df[!is.na(df$value) & df$variable != "Survey",],
aes(linetype=variable, color = variable), size = 1, linetype = "dashed")+
geom_point(data = df[!is.na(df$value) & df$variable == "Survey",],
aes(color = variable), size = 4) +
scale_color_manual(values=c('#999999', 'orange2','turquoise2','blue2'))+
guides(color = guide_legend(override.aes = list(linetype = c("blank", "dashed", "dashed", "dashed"),
shape = c(16, NA, NA, NA)))) +
scale_y_continuous(
breaks=seq(0,100, 10), labels = seq(0, 100, 10), limits=c(0,70),
sec.axis = dup_axis()) +
theme(
legend.position = 'bottom', legend.direction = 'horizontal',
panel.grid.major.y = element_line(color='gray85'),
axis.title = element_text(face='bold')) +
labs(x='Year', y='measure (%)')
Created on 2020-07-11 by the reprex package (v0.3.0)
Output
Objective: Sequence in the legend and respective plots must be as follows: c("Survey", "WhatIf", "Projected1", "Projected2" )
I have tried the following methods alternatively but there's no difference in the output.
df$variable <- factor(df$variable, levels = c("Survey", "WhatIf", "Projected1", "Projected2" ))
scale_fill_discrete(breaks = c("Survey", "WhatIf", "Projected1", "Projected2" ))
I might be missing out on a trivial step and any suggestions would be greatly helpful.
You just need to add a breaks = argument to scale_color_manual and change the order of values = to match because you have the guide argument set to color =:
scale_color_manual(breaks = c("Survey", "WhatIf", "Projected1", "Projected2" ),
values=c('turquoise2','blue2','#999999', 'orange2'))+

Adding legend to ggplot with geom_line with factor color and manually added line

I can see that there are a lot of questions similar to this, but I cant find solution for my particular problem.
Data:
risk_accum <- structure(list(date = structure(c(1465948800, 1465952400, 1465956000,
1465959600, 1465963200, 1465966800, 1465970400, 1465974000, 1465977600,
1465981200, 1465984800, 1465988400, 1465992000, 1465995600, 1465999200,
1466002800, 1466006400, 1466010000, 1466013600, 1466017200, 1466020800,
1466024400, 1466028000, 1466031600, 1466035200, 1466038800, 1466042400,
1466046000, 1466049600, 1466053200, 1466056800, 1466060400, 1466064000,
1466067600, 1466071200, 1466074800, 1466078400, 1466082000, 1466085600,
1466089200, 1466092800, 1466096400, 1466100000, 1466103600, 1466107200,
1466110800, 1466114400, 1466118000, 1466121600, 1466125200, 1466128800,
1466132400, 1466136000, 1466139600, 1466143200, 1466146800, 1466150400,
1466154000, 1466157600, 1466161200, 1466164800, 1466168400, 1466172000,
1466175600, 1466179200, 1466182800, 1466186400, 1466190000, 1466193600,
1466197200, 1466200800, 1466204400, 1466208000, 1466211600, 1466215200,
1466218800, 1466222400, 1466226000, 1466229600, 1466233200, 1466236800,
1466240400, 1466244000, 1466247600, 1466251200, 1466254800, 1466258400,
1466262000, 1466265600, 1466269200, 1466272800, 1466276400, 1466280000,
1466283600, 1466287200, 1466290800, 1466294400, 1466298000, 1466301600,
1466305200, 1466308800, 1466312400, 1466316000, 1466319600, 1466323200,
1466326800, 1466330400, 1466334000, 1466337600, 1466341200, 1466344800,
1466348400, 1466352000, 1466355600, 1466359200, 1466362800, 1466366400,
1466370000, 1466373600, 1466377200), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), risk = c(NA, NA, NA, 1, 2, 3, 4, 5, 6, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 2, 3, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, NA, NA)), .Names = c("date",
"risk"), row.names = c(NA, -120L), class = c("tbl_df", "tbl",
"data.frame"))
And code to generate graph:
#color variable
color_var <- vector(mode = "double",length = length(risk_accum$risk))
color_var[color_var== '0']<-NA
color_var[risk_accum$risk<6] <- "green4"
color_var[risk_accum$risk>=6 & risk_accum$risk<12] <- "yellow2"
color_var[risk_accum$risk>=12] <- "red"
#plot of Effective Blight Hours accumulation
ggplot(risk_accum)+
geom_line(aes(x = date, y = risk), color = color_var)+
scale_y_continuous(name = "EBH accumulation")+
scale_colour_manual(values=c("green", "yellow", "red"))+
geom_line(aes(date, y= 12), linetype= "dotted", size = 0.1)+
theme(axis.title.x = element_blank())
I need to get a legend which would explain the traffic light system (red is danger, etc) and manually added threshold risk line.
Add your color variable to the dataset, map to that variable inside aes, and use scale_*_identity to directly use the colors.
risk_accum$color_var <- NA
risk_accum$color_var[risk_accum$risk<6] <- "green4"
risk_accum$color_var[risk_accum$risk>=6 & risk_accum$risk<12] <- "yellow2"
risk_accum$color_var[risk_accum$risk>=12] <- "red"
ggplot(risk_accum)+
geom_line(aes(x = date, y = risk, color = color_var)) +
scale_y_continuous(name = "EBH accumulation")+
scale_color_identity(guide = 'legend') +
geom_line(aes(date, y= 12), linetype= "dotted", size = 0.1)+
theme(axis.title.x = element_blank())
You can also add your threshold to the legend:
ggplot(risk_accum)+
geom_line(aes(x = date, y = risk, color = color_var)) +
geom_line(aes(date, y= 12, linetype = "threshold"), size = 0.1)+
scale_y_continuous(name = "EBH accumulation")+
scale_color_identity(guide = 'legend') +
scale_linetype_manual(values = 2) +
theme(axis.title.x = element_blank())

How to use geom ribbon when NA values are present?

What I want to do:
I want to plot few curves and fill in the areas between them
The problem
I use geom_ribbon() to fill in the areas. But it fills in more than what I want.
Incorrectly filled white area:
I want to fill in white only between "OPDV" and "SDV" lines (shown in plot).
Code and Data
I used following code for above plot:
library(ggplot2)
library(dplyr)
ggplot() +
geom_ribbon(data = ddf,aes(ymin=BX,ymax=60, x=dv), fill="green") +
geom_ribbon(data = ddf,aes(ymin=BX,ymax=s, x=SDV_1), fill="orange") +
geom_ribbon(data = ddf,aes(ymin=BX,ymax=SDX_1, x=dv), fill="white") +
geom_path(data = ddf,mapping = aes(x = CLDV_1, y = s), size=0.5)+
geom_path(data = ddf,mapping = aes(x = OPDV_1, y = s), size=0.5) +
geom_path(data = ddf,aes(x = SDV_1, y = s), size=0.5) +
#geom_path(data = ddf,aes(x = dv, y = AX), size=0.5) +
geom_path(data = ddf,aes(x = dv, y = BX), size=0.5) +
geom_path(data = ddf,aes(x = dv, y = SDX_1), size=0.5) +
annotate(geom = "text", x = -0.8, y = 29, label = "OPDV",size = 3) +
annotate(geom = "text", x = 1.5, y = 40, label = "SDV",size = 3) +
labs(y = "Spacing (m)", x = "Relative Speed (Vf - Vl), m/s") +
coord_cartesian(ylim = c(25, 50),
xlim = c(-2,3.2))
I also tried replacing the third line with following:
geom_ribbon(data = ddf %>%
filter(dv>OPDV_1 & dv<SDV_1),
aes(ymin=BX,ymax=SDX_1, x=dv), fill="white")
But, since some of the values in OPDV_1 and SDV_1 are NA, it didn't fill in white at all.
Following are my data:
structure(list(BX = c(27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.4804347826087, 27.4295652173913,
27.3786956521739, 27.3278260869565, 27.2769565217391, 27.2260869565217,
27.1752173913043, 27.124347826087, 27.0734782608696, 27.0226086956522,
26.9717391304348, 26.9208695652174, 26.87, 26.8191304347826,
26.7682608695652, 26.7173913043478, 26.6665217391304, 26.615652173913,
26.5647826086957, 26.5139130434783, 26.4630434782609, 26.4121739130435,
26.3613043478261, 26.3104347826087, 26.2595652173913, 26.2086956521739,
26.1578260869565, 26.1069565217391, 26.0560869565217, 26.0052173913043,
25.954347826087, 25.9034782608696, 25.8526086956522, 25.8017391304348,
25.7508695652174, 25.7), dv = c(3.2, 3.14347826086956, 3.08695652173913,
3.03043478260869, 2.97391304347826, 2.91739130434782, 2.86086956521739,
2.80434782608696, 2.74782608695652, 2.69130434782609, 2.63478260869565,
2.57826086956522, 2.52173913043478, 2.46521739130435, 2.40869565217391,
2.35217391304348, 2.29565217391304, 2.23913043478261, 2.18260869565217,
2.12608695652174, 2.0695652173913, 2.01304347826087, 1.95652173913043,
1.9, 1.84347826086956, 1.78695652173913, 1.7304347826087, 1.67391304347826,
1.61739130434783, 1.56086956521739, 1.50434782608696, 1.44782608695652,
1.39130434782609, 1.33478260869565, 1.27826086956522, 1.22173913043478,
1.16521739130435, 1.10869565217391, 1.05217391304348, 0.995652173913044,
0.939130434782609, 0.882608695652173, 0.826086956521738, 0.769565217391303,
0.713043478260868, 0.656521739130433, 0.600000000000001, 0.543478260869566,
0.486956521739131, 0.430434782608696, 0.373913043478261, 0.317391304347826,
0.260869565217391, 0.204347826086956, 0.14782608695652, 0.0913043478260853,
0.0347826086956502, -0.0217391304347814, -0.0782608695652165,
-0.134782608695652, -0.191304347826087, -0.247826086956522, -0.304347826086957,
-0.360869565217392, -0.417391304347827, -0.473913043478262, -0.530434782608694,
-0.586956521739133, -0.643478260869564, -0.699999999999999, -0.756521739130434,
-0.81304347826087, -0.869565217391305, -0.92608695652174, -0.982608695652175,
-1.03913043478261, -1.09565217391305, -1.15217391304348, -1.20869565217392,
-1.26521739130435, -1.32173913043478, -1.37826086956522, -1.43478260869565,
-1.49130434782609, -1.54782608695652, -1.60434782608696, -1.66086956521739,
-1.71739130434783, -1.77391304347826, -1.83043478260869, -1.88695652173913,
-1.94347826086956, -2), s = 8:100, SDV_1 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
-0.0875, 0.0375, 0.1625, 0.2875, 0.4125, 0.5375, 0.6625, 0.7875,
0.9125, 1.0375, 1.1625, 1.2875, 1.4125, 1.5375, 1.6625, 1.7875,
1.9125, 2.0375, 2.1625, 2.2875, 2.4125, 2.5375, 2.6625, 2.7875,
2.9125, 3.0375, 3.1625, 3.2875, 3.4125, 3.5375, 3.6625, 3.7875,
3.9125, 4.0375, 4.1625, 4.2875, 4.4125, 4.53994565217391, 4.67130434782609,
4.80266304347826, 4.93402173913043, 5.06538043478261, 5.19673913043478,
5.32809782608696, 5.45945652173913, 5.5908152173913, 5.72217391304348,
5.85353260869565, 5.98489130434783, 6.11625, 6.24760869565217,
6.37896739130435, 6.51032608695652, 6.6416847826087, 6.77304347826087,
6.90440217391304, 7.03576086956522, 7.16711956521739, 7.29847826086957,
7.42983695652174, 7.56119565217391, 7.69255434782609, 7.82391304347826,
7.95527173913043, 8.08663043478261, 8.21798913043478, 8.34934782608696,
8.48070652173913, 8.6120652173913, 8.74342391304348, 8.87478260869565,
9.00614130434783, 9.1375), SDX_1 = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 31.5, 31.5, 31.5,
31.5, 31.5, 31.5, 31.5, 31.4804347826087, 31.4295652173913, 31.3786956521739,
31.3278260869565, 31.2769565217391, 31.2260869565217, 31.1752173913043,
31.124347826087, 31.0734782608696, 31.0226086956522, 30.9717391304348,
30.9208695652174, 30.87, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), CLDV_1 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0.619176470588235, 0.646767058823529, 0.675703529411765,
0.705985882352941, 0.737614117647059, 0.770588235294118, 0.804908235294118,
0.840574117647059, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), OPDV_1 = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.619176470588235,
-0.646767058823529, -0.675703529411765, -0.705985882352941, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA)), row.names = c(NA, -93L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("BX", "dv", "s", "SDV_1", "SDX_1",
"CLDV_1", "OPDV_1"))
Please guide me if there is any other function in ggplot2 or extensions that I can use to fill in correctly. Or, if possible, how can I use geom_ribbon() effectively in this case?
Use geom_polygon to fill the white area between the OPDV, SDX, SDV and BX curves.
Create data to draw the white polygon
library(tidyr)
# Change data to a long format
ddflong <- ddf %>%
gather(key, value, -dv, -s)
# Extract data for each polygon side
sideOPDV <- ddflong %>%
filter(key =="OPDV_1" & !is.na(value)) %>%
transmute(x = value, y = s, key = key) %>%
arrange(y)
sideSDX <- ddflong %>%
filter(key =="SDX_1" & !is.na(value)) %>%
transmute(x = dv, y = value, key = key) %>%
arrange(x)
sideSDV <- ddflong %>%
filter(key =="SDV_1" & !is.na(value) & value < max(sideSDX$x)) %>%
transmute(x = value, y = s, key = key) %>%
arrange(desc(y))
sideBX <- ddflong %>%
filter(key == "BX" & dv > max(sideOPDV$x) & dv < min(sideSDV$x)) %>%
transmute(x = dv, y = value, key = key) %>%
arrange(desc(x))
# Combine all sides in one polygon
datapolygon <- rbind(sideOPDV, sideSDX, sideSDV, sideBX)
Draw the plot
ggplot(data = ddf) +
geom_ribbon(aes(ymin=BX,ymax=60, x=dv), fill="green") +
geom_ribbon(aes(ymin=BX,ymax=s, x=SDV_1), fill="orange") +
#### Here is the new instruction ####
geom_polygon(data = datapolygon, aes(x = x, y =y), fill="white") +
# Added colours to identify the lines
geom_path(aes(x = CLDV_1, y = s), colour = "yellow")+
geom_path(aes(x = OPDV_1, y = s), colour = "purple") +
geom_path(aes(x = SDV_1, y = s), colour = "dark green") +
geom_path(aes(x = dv, y = BX), colour = "blue") +
geom_path(aes(x = dv, y = SDX_1), colour = "red") +
annotate(geom = "text", x = 1, y = 30, label = "CLDV", colour = "yellow") +
annotate(geom = "text", x = -0.9, y = 29, label = "OPDV", colour = "purple") +
annotate(geom = "text", x = 1.2, y = 40, label = "SDV", colour = "dark green") +
annotate(geom = "text", x = -0.2, y = 26, label = "BX", colour = "blue") +
annotate(geom = "text", x = -0.3, y = 32, label = "SDX", colour = "red") +
labs(y = "Spacing (m)", x = "Relative Speed (Vf - Vl), m/s") +
coord_cartesian(ylim = c(25, 50),
xlim = c(-2,3.2))

How to change xticks locations and customize legend using levelplot (lattice library)

I am trying to move the position of x-ticks and x-labels from the bottom of the figure to its top.
In addition, my data has a bunch of NAs. Currently, levelplot just remove them and leave them as white space in the plot. I wondering if it is possible to add this NAs to the legend as well.
Any suggestions? Thanks!
Here is my code and its output:
require(lattice)
# see data from dput() below
rownames(data)=data[,1]
data_matrix=as.matrix(data[,2:11])
color = colorRampPalette(rev(c("#D73027", "#FC8D59", "#FEE090", "#FFFFBF", "#E0F3F8", "#91BFDB", "#4575B4")))(100)
levelplot(data_matrix, scale=list(x=list(rot=45)), ylab="Days", xlab="Strains", col.regions = color)
Data
data <-
structure(list(X = structure(1:17, .Label = c("Arcobacter", "Bacillus",
"Bordetella", "Campylobacter", "Chlamydia", "Clostridium ", "Corynebacterium",
"Enterococcus", "Escherichia", "Francisella", "Legionella", "Mycobacterium",
"Pseudomonas", "Rickettsia", "Staphylococcus", "Streptococcus",
"Treponema"), class = "factor"), day.0 = c(NA, -3.823301154,
NA, NA, NA, -3.518606107, NA, NA, NA, NA, NA, -4.859479387, NA,
NA, NA, -2.588402346, -2.668136603), day.2 = c(-4.006281239,
-3.024823788, NA, -5.202804501, NA, -3.237622321, NA, NA, -5.296138823,
-5.105469059, NA, NA, -4.901775198, NA, NA, -2.979144202, -3.050083791
), day.4 = c(-2.880770182, -3.210165554, -4.749097175, -5.209064234,
NA, -2.946480184, NA, -5.264113795, -5.341881713, -4.435780293,
NA, -4.810650076, -4.152531609, NA, NA, -3.106172794, -3.543161966
), day.6 = c(-2.869833226, -3.293283924, -3.831346387, NA, NA,
-3.323947791, NA, NA, NA, NA, NA, -4.397581863, -4.068855504,
NA, NA, -3.27028378, -3.662618619), day.8 = c(-3.873589331, -3.446192193,
-3.616207965, NA, NA, -3.13869325, NA, -5.010807453, NA, NA,
NA, -4.091502649, -4.412399025, -4.681675749, NA, -3.404738625,
-3.955464159), day.15 = c(-5.176583159, -2.512963066, -3.392832457,
NA, NA, -3.194662968, NA, -3.60440455, NA, NA, -4.875554468,
-2.507376205, -4.727255906, -5.27116754, -3.200499549, -3.361296145,
-4.320554841), day.22 = c(-4.550052847, -3.654013004, -3.486879661,
NA, NA, -3.614890858, NA, NA, NA, NA, -4.706690492, -2.200533317,
-4.836957953, NA, -4.390423731, NA, NA), day.29 = c(-4.730006329,
-3.46707372, -3.594457287, NA, NA, -3.800757834, NA, NA, NA,
NA, -4.285154089, -2.121152491, -4.816807055, -5.064577888, -2.945243736,
-4.479177287, -5.226435146), day.43 = c(-4.398680025, -3.144603215,
-3.642065153, NA, NA, -3.8268662, NA, NA, NA, NA, -4.762539208,
-2.156862316, -4.118608495, NA, -4.030291084, -4.678213147, NA
), day.57 = c(-4.689982547, -2.713502214, -3.51279797, NA, -5.069579266,
-3.495580794, NA, NA, NA, NA, -4.515973639, -1.90591075, -4.134826117,
-4.479351427, -3.482134037, -4.538534489, NA)), .Names = c("X",
"day.0", "day.2", "day.4", "day.6", "day.8", "day.15", "day.22",
"day.29", "day.43", "day.57"), class = "data.frame", row.names = c("Arcobacter",
"Bacillus", "Bordetella", "Campylobacter", "Chlamydia", "Clostridium ",
"Corynebacterium", "Enterococcus", "Escherichia", "Francisella",
"Legionella", "Mycobacterium", "Pseudomonas", "Rickettsia", "Staphylococcus",
"Streptococcus", "Treponema"))
Figure
The request to move the labels to the top is pretty easy (after looking at the ?xyplot under the scales section):
levelplot(data_matrix, scale=list(x=list(rot=45,alternating=2)),
ylab="Days", xlab="Strains", col.regions = color)
Trying to get the NA values into the color legend may take a bit more thinking, but it seems as though sensible values for the colorkey arguments for at and col might suffice.
levelplot(data_matrix, scale=list(x=list(rot=45,alternating=2)),
ylab="Days", xlab="Strains", col.regions = color,
colorkey=list(at=as.numeric( factor( c( seq(-5.5, -2, by=0.5),
"NA"))),
labels=as.character( c( seq(-5.5, -2, by=0.5),
"NA")),
col=c(color, "#FFFFFF") ) )

Resources