Reputation: 500
I want to display a graph showing a progression, filling two different areas depending in different y variable. I did the following:
library(tidyverse)
data_processed_esp %>%
ggplot(aes(x = index, y = cases)) +
geom_area(aes(fill = "a", alpha = 0.3)) +
geom_area(aes(x = index , y = deaths, fill = "b")) +
scale_fill_manual(values=c("#000000", "#FF0000")) +
theme(legend.position = "None") + scale_y_continuous(breaks = seq(0,9000,1000))
There are several things that I don't like with this. First of all, I added two geom_area, but I wanted to add a color to the fill directly. This didn't work, so I put some names to the fill values and later I scaled them. I also had to remove the legend position, because the fill value is showing up. Finally, I wanted to add some transparency, but adding alpha to one area results in adding it indirectly to the other one.
What is the proper way to achieve this?
dput
structure(list(dateRep = structure(c(83L, 86L, 3L, 6L, 9L, 12L,
15L, 18L, 21L, 24L, 27L, 30L, 33L, 36L, 39L, 42L, 45L, 48L, 51L,
54L, 57L, 60L, 63L, 66L, 69L, 72L, 75L, 78L, 81L, 84L), .Label = c("01/01/2020",
"01/02/2020", "01/03/2020", "02/01/2020", "02/02/2020", "02/03/2020",
"03/01/2020", "03/02/2020", "03/03/2020", "04/01/2020", "04/02/2020",
"04/03/2020", "05/01/2020", "05/02/2020", "05/03/2020", "06/01/2020",
"06/02/2020", "06/03/2020", "07/01/2020", "07/02/2020", "07/03/2020",
"08/01/2020", "08/02/2020", "08/03/2020", "09/01/2020", "09/02/2020",
"09/03/2020", "10/01/2020", "10/02/2020", "10/03/2020", "11/01/2020",
"11/02/2020", "11/03/2020", "12/01/2020", "12/02/2020", "12/03/2020",
"13/01/2020", "13/02/2020", "13/03/2020", "14/01/2020", "14/02/2020",
"14/03/2020", "15/01/2020", "15/02/2020", "15/03/2020", "16/01/2020",
"16/02/2020", "16/03/2020", "17/01/2020", "17/02/2020", "17/03/2020",
"18/01/2020", "18/02/2020", "18/03/2020", "19/01/2020", "19/02/2020",
"19/03/2020", "20/01/2020", "20/02/2020", "20/03/2020", "21/01/2020",
"21/02/2020", "21/03/2020", "22/01/2020", "22/02/2020", "22/03/2020",
"23/01/2020", "23/02/2020", "23/03/2020", "24/01/2020", "24/02/2020",
"24/03/2020", "25/01/2020", "25/02/2020", "25/03/2020", "26/01/2020",
"26/02/2020", "26/03/2020", "27/01/2020", "27/02/2020", "27/03/2020",
"28/01/2020", "28/02/2020", "28/03/2020", "29/01/2020", "29/02/2020",
"30/01/2020", "31/01/2020", "31/12/2019"), class = "factor"),
day = c(28L, 29L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L), month = c(2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), year = c(2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L), cases = c(13L, 9L, 32L, 17L, 31L, 37L, 49L,
61L, 113L, 56L, 159L, 615L, 435L, 501L, 864L, 1227L, 1522L,
2000L, 1438L, 1987L, 2538L, 3431L, 2833L, 4946L, 3646L, 4517L,
6584L, 7937L, 8578L, 7871L), deaths = c(0L, 0L, 0L, 0L, 0L,
0L, 1L, 2L, 2L, 0L, 0L, 23L, 7L, 12L, 37L, 37L, 15L, 152L,
21L, 182L, 107L, 169L, 235L, 324L, 394L, 462L, 514L, 738L,
655L, 769L), countriesAndTerritories = structure(c(169L,
169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L,
169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L,
169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L, 169L), .Label = c("Afghanistan",
"Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua_and_Barbuda",
"Argentina", "Armenia", "Aruba", "Australia", "Austria",
"Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados",
"Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
"Bolivia", "Bosnia_and_Herzegovina", "Brazil", "British_Virgin_Islands",
"Brunei_Darussalam", "Bulgaria", "Burkina_Faso", "Cambodia",
"Cameroon", "Canada", "Cape_Verde", "Cases_on_an_international_conveyance_Japan",
"Cayman_Islands", "Central_African_Republic", "Chad", "Chile",
"China", "Colombia", "Congo", "Costa_Rica", "Cote_dIvoire",
"Croatia", "Cuba", "Curaçao", "Cyprus", "Czech_Republic",
"Democratic_Republic_of_the_Congo", "Denmark", "Djibouti",
"Dominica", "Dominican_Republic", "Ecuador", "Egypt", "El_Salvador",
"Equatorial_Guinea", "Eritrea", "Estonia", "Eswatini", "Ethiopia",
"Faroe_Islands", "Fiji", "Finland", "France", "French_Polynesia",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar",
"Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guernsey",
"Guinea", "Guinea_Bissau", "Guyana", "Haiti", "Holy_See",
"Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran",
"Iraq", "Ireland", "Isle_of_Man", "Israel", "Italy", "Jamaica",
"Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kosovo",
"Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Liberia",
"Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Madagascar",
"Malaysia", "Maldives", "Mali", "Malta", "Mauritania", "Mauritius",
"Mexico", "Moldova", "Monaco", "Mongolia", "Montenegro",
"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
"Nepal", "Netherlands", "New_Caledonia", "New_Zealand", "Nicaragua",
"Niger", "Nigeria", "North_Macedonia", "Norway", "Oman",
"Pakistan", "Palestine", "Panama", "Papua_New_Guinea", "Paraguay",
"Peru", "Philippines", "Poland", "Portugal", "Puerto_Rico",
"Qatar", "Romania", "Russia", "Rwanda", "Saint_Kitts_and_Nevis",
"Saint_Lucia", "Saint_Vincent_and_the_Grenadines", "San_Marino",
"Saudi_Arabia", "Senegal", "Serbia", "Seychelles", "Singapore",
"Sint_Maarten", "Slovakia", "Slovenia", "Somalia", "South_Africa",
"South_Korea", "Spain", "Sri_Lanka", "Sudan", "Suriname",
"Sweden", "Switzerland", "Syria", "Taiwan", "Thailand", "Timor_Leste",
"Togo", "Trinidad_and_Tobago", "Tunisia", "Turkey", "Turks_and_Caicos_islands",
"Uganda", "Ukraine", "United_Arab_Emirates", "United_Kingdom",
"United_Republic_of_Tanzania", "United_States_of_America",
"United_States_Virgin_Islands", "Uruguay", "Uzbekistan",
"Venezuela", "Vietnam", "Zambia", "Zimbabwe"), class = "factor"),
geoId = structure(c(57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L,
57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L,
57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L, 57L), .Label = c("AD",
"AE", "AF", "AG", "AI", "AL", "AM", "AO", "AR", "AT", "AU",
"AW", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BJ",
"BM", "BN", "BO", "BR", "BS", "BT", "BY", "BZ", "CA", "CD",
"CF", "CG", "CH", "CI", "CL", "CM", "CN", "CO", "CR", "CU",
"CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ",
"EC", "EE", "EG", "EL", "ER", "ES", "ET", "FI", "FJ", "FO",
"FR", "GA", "GD", "GE", "GG", "GH", "GI", "GL", "GM", "GN",
"GQ", "GT", "GU", "GW", "GY", "HN", "HR", "HT", "HU", "ID",
"IE", "IL", "IM", "IN", "IQ", "IR", "IS", "IT", "JE", "JM",
"JO", "JP", "JPG11668", "KE", "KG", "KH", "KN", "KR", "KW",
"KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LT", "LU",
"LV", "LY", "MA", "MC", "MD", "ME", "MG", "MK", "ML", "MM",
"MN", "MR", "MS", "MT", "MU", "MV", "MX", "MY", "MZ", "NC",
"NE", "NG", "NI", "NL", "NO", "NP", "NZ", "OM", "PA", "PE",
"PF", "PG", "PH", "PK", "PL", "PR", "PS", "PT", "PY", "QA",
"RO", "RS", "RU", "RW", "SA", "SC", "SD", "SE", "SG", "SI",
"SK", "SM", "SN", "SO", "SR", "SV", "SX", "SY", "SZ", "TC",
"TD", "TG", "TH", "TL", "TN", "TR", "TT", "TW", "TZ", "UA",
"UG", "UK", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI",
"VN", "XK", "ZA", "ZM", "ZW"), class = "factor"), countryterritoryCode = structure(c(56L,
56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L,
56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L,
56L, 56L, 56L, 56L, 56L), .Label = c("", "ABW", "AFG", "AGO",
"ALB", "AND", "ARE", "ARG", "ARM", "ATG", "AUS", "AUT", "AZE",
"BEL", "BEN", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLR",
"BLZ", "BMU", "BOL", "BRA", "BRB", "BRN", "BTN", "CAF", "CAN",
"CHE", "CHL", "CHN", "CIV", "CMR", "COD", "COG", "COL", "CPV",
"CRI", "CUB", "CUW", "CYM", "CYP", "CZE", "DEU", "DJI", "DMA",
"DNK", "DOM", "DZA", "ECU", "EGY", "ERI", "ESP", "EST", "ETH",
"FIN", "FJI", "FRA", "FRO", "GAB", "GBR", "GEO", "GGY", "GHA",
"GIB", "GIN", "GMB", "GNB", "GNQ", "GRC", "GRD", "GRL", "GTM",
"GUM", "GUY", "HND", "HRV", "HTI", "HUN", "IDN", "IMN", "IND",
"IRL", "IRN", "IRQ", "ISL", "ISR", "ITA", "JAM", "JEY", "JOR",
"JPN", "KAZ", "KEN", "KGZ", "KHM", "KNA", "KOR", "KWT", "LAO",
"LBN", "LBR", "LBY", "LCA", "LIE", "LKA", "LTU", "LUX", "LVA",
"MAR", "MCO", "MDA", "MDG", "MDV", "MEX", "MKD", "MLI", "MLT",
"MMR", "MNE", "MNG", "MOZ", "MRT", "MSR", "MUS", "MYS", "N/A",
"NAM", "NCL", "NER", "NGA", "NIC", "NLD", "NOR", "NPL", "NZL",
"OMN", "PAK", "PAN", "PER", "PHL", "PNG", "POL", "PRI", "PRT",
"PRY", "PSE", "PYF", "QAT", "ROU", "RUS", "RWA", "SAU", "SDN",
"SEN", "SGP", "SLV", "SMR", "SOM", "SRB", "SUR", "SVK", "SVN",
"SWE", "SWZ", "SXM", "SYC", "SYR", "TCA", "TCD", "TGO", "THA",
"TLS", "TTO", "TUN", "TUR", "TWN", "TZA", "UGA", "UKR", "URY",
"USA", "UZB", "VAT", "VCT", "VEN", "VGB", "VIR", "VNM", "XKX",
"ZAF", "ZMB", "ZWE"), class = "factor"), popData2018 = c(46723749L,
46723749L, 46723749L, 46723749L, 46723749L, 46723749L, 46723749L,
46723749L, 46723749L, 46723749L, 46723749L, 46723749L, 46723749L,
46723749L, 46723749L, 46723749L, 46723749L, 46723749L, 46723749L,
46723749L, 46723749L, 46723749L, 46723749L, 46723749L, 46723749L,
46723749L, 46723749L, 46723749L, 46723749L, 46723749L), index = 60:89), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -30L))
Upvotes: 2
Views: 1299
Reputation: 12703
df
is your data
library('data.table')
df1 <- melt(df, measure.vars = c('cases', 'deaths'), id.vars = 'index')
library('ggplot2')
ggplot(data = df1, mapping = aes(x = index, y = value, group = variable)) +
geom_area(data = subset(df1, variable == 'cases'), aes(color = variable, fill = variable), alpha = 0.3) +
geom_area(data = subset(df1, variable == 'deaths'), aes(color = variable, fill = variable), alpha = 0.5) +
scale_color_manual(values=c("#000000", "#FF0000")) +
scale_fill_manual(values=c("#000000", "#FF0000"))
Upvotes: 1
Reputation: 16178
You can reshape the two columns of interest (here "deaths" and "cases") into a longer format (using for example pivot_longer
function from tidyr
) and then use this new categorical variable in the argument of fill
and alpha
into your aes
:
library(tidyverse)
df %>% pivot_longer(cols = c(deaths,cases), values_to = "val", names_to = "var") %>%
ggplot(aes(x = index, y = val, fill = var, alpha = var))+
geom_area()+
scale_fill_manual(values=c("#000000", "#FF0000"))+
scale_alpha_manual(values= c(1,0.3)) +
scale_y_continuous(breaks = seq(0,9000,1000))
Does it look what you are trying to achieve ?
Upvotes: 0