How can i add Hatched polygons on a spplot in R? - r
I have a map which summarizes an indicator of the saturation percentage of real estate by neighborhood in Paris (Observed Price of real estate/maximum price set by law). I would like to add hatched on neighborhoods which have less than 5 observations included in my dataset.
I searched, but I couldn't find a way to do it. Any advice in the right direction is welcomed. Thanks.
Here is my code:
library(sp)
library(sf)
library(rgdal)
library(RColorBrewer)
library(raster)
library(classInt)
library(cartography)
#Importation
setwd("path")
shp <- readOGR(dsn="path/to/file",layer="l_qu_paris")
#Breaks
q10 <- classIntervals(map$saturation2, n=7, style="fixed",
fixedBreaks=c(45,69.999999, 79.9999999, 89.9999999, 99.9999999
,109.99999999, 120))
#Colors
my.palette <- colors()[c(73,26,128,10,652,92)]
#Map
##Scale
scale.parameter = 1.1
xshift = 0
yshift = 0
original.bbox = shp#bbox
edges = original.bbox
edges[1, ] <- (edges[1, ] - mean(edges[1, ])) * scale.parameter + mean(edges[1, ]) + xshift
edges[2, ] <- (edges[2, ] - mean(edges[2, ])) * scale.parameter + mean(edges[2, ]) + yshift
#Saturation
idx <- match(shp$l_qu, map$l_qu)
is.na(idx)
concordance <- map[idx, "saturation2"]
shp$saturation2 <- concordance
spplot(shp, "saturation2",col.regions=my.palette,
col = "black", lwd= 1, at = q10$brks,
main=list(label="% de saturation des meublés 1 pièce",cex=1.2,fontfamily="serif"),
xlim = edges[1, ], ylim = edges[2, ])
grid.text("Saturation moyenne (en%)", x=unit(0.95, "npc"), y=unit(0.50, "npc"), rot=90)
Here is my map:
saturation
Here is an example of a map that i would like to have:
saturation example
Here are the polygons in shapefile format: https://www.data.gouv.fr/fr/datasets/quartiers-administratifs/
And here is my dataset:
map <- structure(list(l_qu = c("Amérique", "Archives", "Arsenal", "Arts-et-Métiers",
"Auteuil", "Batignolles", "Bel-Air", "Belleville", "Bercy", "Bonne-Nouvelle",
"Chaillot", "Champs-Elysées", "Charonne", "Chaussée-d'Antin",
"Clignancourt", "Combat", "Croulebarbe", "Ecole-Militaire", "Enfants-Rouges",
"Epinettes", "Europe", "Faubourg-du-Roule", "Faubourg-Montmartre",
"Folie-Méricourt", "Gaillon", "Gare", "Goutte-d'Or", "Grandes-Carrières",
"Grenelle", "Gros-Caillou", "Halles", "Hôpital-Saint-Louis",
"Invalides", "Jardin-des-Plantes", "Javel", "La Chapelle", "Madeleine",
"Mail", "Maison-Blanche", "Monnaie", "Montparnasse", "Muette",
"Necker", "Notre-Dame", "Notre-Dame-des-Champs", "Odéon", "Palais-Royal",
"Parc-de-Montsouris", "Père-Lachaise", "Petit-Montrouge", "Picpus",
"Place-Vendôme", "Plaine de Monceaux", "Plaisance", "Pont-de-Flandre",
"Porte-Dauphine", "Porte-Saint-Denis", "Porte-Saint-Martin",
"Quinze-Vingts", "Rochechouart", "Roquette", "Saint-Ambroise",
"Saint-Fargeau", "Saint-Germain-des-Prés", "Saint-Gervais",
"Saint-Lambert", "Saint-Merri", "Saint-Thomas-d'Aquin", "Saint-Victor",
"Saint-Vincent-de-Paul", "Sainte-Avoie", "Sainte-Marguerite",
"Saint-Georges", "Salpêtrière", "Sorbonne", "Saint-Germain-l'Auxerrois",
"Ternes", "Val-de-Grâce", "Villette", "Vivienne", "Total"),
saturation2 = c(98.188951329533, 85.4938271604938, 83.8463463463464,
90.1460755525873, 98.1726527090667, 90.2186740262059, 92.8743271072797,
72.8549079897508, 99.2356140350877, 90.1234567901235, 114.057904044022,
NA, 87.2208980972528, 91.2562612612613, 97.9518951016991,
86.2770900920801, 91.0239726151895, 92.8305400372439, 88.6514719848053,
73.876877752942, 108.693318725755, 67.3263578578579, 85.8735259484408,
89.2100224414912, 92, 90.6120989320281, 85.8446948520848,
91.4165103088783, 97.2760978594495, 93.60892313074, 102.471730530348,
95.9062868379746, 96, 92.5484278273071, 95.0066946433545,
85.8187074829932, 101.139150713213, 92.1272297297297, 93.0625144594594,
61.8074324324324, 100.173302938197, 99.720856146949, 84.8732544128823,
84.1911355800245, 85.1122672253259, 91.8422003734504, NA,
94.612349767814, 83.2363741480137, 87.0403187718064, 92.0886931496388,
77, 110.943302180685, 100.73486307088, 66.3899425287356,
96.2527514568292, 95.7430893746874, 87.9028997984617, 48,
85.5630809345015, 92.7010730078939, 82.075822827797, 83.1727736726875,
76.2162162162162, 104.534662867996, 98.3510353194912, 78.3333333333333,
103.169134078212, 80.8779605984059, 92.63515704154, 62, 90.3902768982325,
94.1391771653151, 94.8669917042241, 94.4825319797959, 95.4279279279279,
98.2238673533848, 94.0602977590835, 87.5105365473892, 102,
92.5123935729199), numobs = c(6, 4, 4, 6, 36, 15, 4, 4, 3,
2, 16, NA, 36, 3, 32, 9, 22, 13, 11, 6, 31, 5, 15, 14, 4,
22, 3, 64, 29, 58, 7, 18, 4, 13, 23, 2, 8, 4, 47, 12, 16,
49, 50, 9, 33, 26, NA, 15, 10, 10, 23, 2, 13, 15, 2, 12,
8, 31, 1, 17, 22, 42, 7, 3, 4, 74, 4, 7, 13, 6, 2, 23, 18,
16, 17, 1, 24, 44, 8, 4, 1290)), row.names = c(NA, -81L), class = c("tbl_df",
"tbl", "data.frame"))
Neither spplot, nor ggplot2 support textured fillings. Having said that, there is a package called ggpattern which provides custom ggplot2 geoms which support filled areas with geometric and image-based patterns. See developer site for more info on ggpattern: https://coolbutuseless.github.io/package/ggpattern/index.html
With ggpattern you can plot 'hatched' or textured geom fillings. Below is a working example from the developers website:
library(maps)
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
crimesm <- reshape2::melt(crimes, id = 1)
states_map <- map_data("state")
p <- ggplot(crimes, aes(map_id = state)) +
geom_map_pattern(
aes(
# fill = Murder,
pattern_fill = Murder,
pattern_spacing = state,
pattern_density = state,
pattern_angle = state,
pattern = state
),
fill = 'white',
colour = 'black',
pattern_aspect_ratio = 1.8,
map = states_map
) +
expand_limits(x = states_map$long, y = states_map$lat) +
coord_map() +
theme_bw(18) +
labs(title = "ggpattern::geom_map_pattern()") +
scale_pattern_density_discrete(range = c(0.01, 0.3)) +
scale_pattern_spacing_discrete(range = c(0.01, 0.03)) +
theme(legend.position = 'none')
p
Related
How to use stat_function or geom_line to add curve to scatterplot
This is a revised question that I posted earlier which has been improved for clarity. I am attempting to make a scatterplot of CO2 rate data (y) at varying temperatures (t) with two categorical variables, depth (Mineral, Organic) and substrate (Calcareous, Metapelite, Peridotite) with fitted lines following the equation y = a*exp(b*t) where y = CO2 rate, a = basal respiration (intercept), b = slope and t = temperature (time equivalent). I have already fitted all of the exponential curves so I have the values for y, a, b and t for each data point. I am struggling to figure out how to plot the exponential curves using the function exp_funct <- function(y,a,b,t){y=a*exp(b*t)} for each category of the two groups, depth and substrate. So far, I have produced the base scatterplot using GGplot2 colouring by depth and faceting by substrate but I do not know what the best approach is to fit the curves, I have attempted using geom_line and stat_function with no success. Here's my attempt at some reproduceable code: co2_data <- structure(list(chamber_temp = c(10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 15, 19, 19, 19, 25, 25, 25, 25, 25, 25, 35, 35, 35, 35, 35, 35, 5, 5, 5, 5, 5, 5), substrate = c("Calcareous", "Metapelite", "Metapelite", "Peridotite", "Peridotite", "Calcareous", "Calcareous", "Metapelite", "Metapelite", "Peridotite", "Peridotite", "Calcareous", "Calcareous", "Metapelite", "Calcareous", "Calcareous", "Metapelite", "Metapelite", "Peridotite", "Peridotite", "Calcareous", "Calcareous", "Metapelite", "Metapelite", "Peridotite", "Peridotite", "Calcareous", "Calcareous", "Metapelite", "Metapelite", "Peridotite", "Peridotite" ), depth = c("Mineral", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic", "Mineral", "Organic"), N.x = c(3, 6, 4, 5, 8, 6, 8, 7, 8, 8, 8, 3, 8, 4, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8), basal_respiration = c(0.0092, 0.0124666666666667, 0.04935, 0.0101, 0.05785, 0.01315, 0.01415, 0.013, 0.0402, 0.01075, 0.05785, 0.0171, 0.01415, 0.03105, 0.01315, 0.01415, 0.013075, 0.0402, 0.01075, 0.05785, 0.01315, 0.01415, 0.013075, 0.0402, 0.01075, 0.05785, 0.01315, 0.01415, 0.013075, 0.0402, 0.01075, 0.05785), sd.x = c(0.00744781847254617, 0.00234065517893317, 0.00178978583448784, 0.00166132477258362, 0.0118691677407113, 0.00666175652512158, 0.00727284577825528, 0.00256059888828115, 0.00986798575481049, 0.00193833507349551, 0.0118691677407113, 0.00294448637286709, 0.00727284577825528, 0.000866025403784439, 0.00666175652512158, 0.00727284577825528, 0.00238012604708238, 0.00986798575481049, 0.00193833507349551, 0.0118691677407113, 0.00666175652512158, 0.00727284577825528, 0.00238012604708238, 0.00986798575481049, 0.00193833507349551, 0.0118691677407113, 0.00666175652512158, 0.00727284577825528, 0.00238012604708238, 0.00986798575481049, 0.00193833507349551, 0.0118691677407113), se.x = c(0.0043, 0.000955568475364854, 0.000894892917243919, 0.000742967024840269, 0.0041963844982488, 0.00271965071286737, 0.00257133928416413, 0.000967815409397198, 0.00348885982193938, 0.000685304937340201, 0.0041963844982488, 0.0017, 0.00257133928416413, 0.00043301270189222, 0.00271965071286737, 0.00257133928416413, 0.000841501633985341, 0.00348885982193938, 0.000685304937340201, 0.0041963844982488, 0.00271965071286737, 0.00257133928416413, 0.000841501633985341, 0.00348885982193938, 0.000685304937340201, 0.0041963844982488, 0.00271965071286737, 0.00257133928416413, 0.000841501633985341, 0.00348885982193938, 0.000685304937340201, 0.0041963844982488), ci.x = c(0.0185014067379227, 0.00245636696547958, 0.00284794865810747, 0.00206280715944113, 0.00992287255356713, 0.00699108472177222, 0.00608025123040774, 0.00236815899497472, 0.00824984254536553, 0.00162048867457091, 0.00992287255356713, 0.00731450964057409, 0.00608025123040774, 0.00137803967327781, 0.00699108472177222, 0.00608025123040774, 0.00198983517147669, 0.00824984254536553, 0.00162048867457091, 0.00992287255356713, 0.00699108472177222, 0.00608025123040774, 0.00198983517147669, 0.00824984254536553, 0.00162048867457091, 0.00992287255356713, 0.00699108472177222, 0.00608025123040774, 0.00198983517147669, 0.00824984254536553, 0.00162048867457091, 0.00992287255356713 ), N.y = c(3, 6, 4, 5, 8, 6, 8, 7, 8, 8, 8, 3, 8, 4, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8), slope = c(0.120293333333333, 0.0593333333333333, 0.07685, 0.05602, 0.067475, 0.108913333333333, 0.15655, 0.0600714285714286, 0.08535, 0.057525, 0.067475, 0.0975333333333333, 0.15655, 0.09385, 0.108913333333333, 0.15655, 0.058125, 0.08535, 0.057525, 0.067475, 0.108913333333333, 0.15655, 0.058125, 0.08535, 0.057525, 0.067475, 0.108913333333333, 0.15655, 0.058125, 0.08535, 0.057525, 0.067475), sd.y = c(0.0326433842199814, 0.00744813175680094, 0.00456106712659804, 0.00374259268422306, 0.00379877799900366, 0.0244087448810189, 0.0131734581640509, 0.00707406396298344, 0.00967780966954816, 0.00357481268240529, 0.00379877799900366, 0.00594670777265315, 0.0131734581640509, 0.00225166604983954, 0.0244087448810189, 0.0131734581640509, 0.0085558250833653, 0.00967780966954816, 0.00357481268240529, 0.00379877799900366, 0.0244087448810189, 0.0131734581640509, 0.0085558250833653, 0.00967780966954816, 0.00357481268240529, 0.00379877799900366, 0.0244087448810189, 0.0131734581640509, 0.0085558250833653, 0.00967780966954816, 0.00357481268240529, 0.00379877799900366), se.y = c(0.0188466666666667, 0.00304068705686359, 0.00228053356329902, 0.00167373833080324, 0.00134307084165888, 0.00996482837004454, 0.00465752079973885, 0.0026737448578026, 0.00342162242218512, 0.00126388714460023, 0.00134307084165888, 0.00343333333333334, 0.00465752079973885, 0.00112583302491977, 0.00996482837004454, 0.00465752079973885, 0.00302494096754678, 0.00342162242218512, 0.00126388714460023, 0.00134307084165888, 0.00996482837004454, 0.00465752079973885, 0.00302494096754678, 0.00342162242218512, 0.00126388714460023, 0.00134307084165888, 0.00996482837004454, 0.00465752079973885, 0.00302494096754678, 0.00342162242218512, 0.00126388714460023, 0.00134307084165888 ), ci.y = c(0.0810906617800115, 0.007816334916228, 0.00725767561259645, 0.00464704259594057, 0.00317585788379371, 0.0256154068032699, 0.0110132866353603, 0.0065424179794951, 0.00809085135929258, 0.00298861819339805, 0.00317585788379371, 0.0147724410388065, 0.0110132866353603, 0.0035829031505223, 0.0256154068032699, 0.0110132866353603, 0.00715284877149766, 0.00809085135929258, 0.00298861819339805, 0.00317585788379371, 0.0256154068032699, 0.0110132866353603, 0.00715284877149766, 0.00809085135929258, 0.00298861819339805, 0.00317585788379371, 0.0256154068032699, 0.0110132866353603, 0.00715284877149766, 0.00809085135929258, 0.00298861819339805, 0.00317585788379371), N = c(3, 6, 4, 5, 8, 6, 8, 7, 8, 8, 8, 3, 8, 4, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8, 6, 8, 8, 8, 8, 8), co2_rate_u_m_h_g = c(0.0303333333333333, 0.0113333333333333, 0.0645, 0.0066, 0.129375, 0.0615, 0.1325, 0.0254285714285714, 0.132, 0.021, 0.14325, 0.085, 0.208, 0.17, 0.198666666666667, 0.71025, 0.0575, 0.344, 0.05225, 0.3115, 0.5155, 3.27125, 0.10375, 0.7835, 0.079, 0.6065, -0.00766666666666667, 0.024625, 0.02675, 0.065125, 0.012125, 0.061), sd = c(0.0161658075373095, 0.0109483636524673, 0.0137719521734091, 0.00634822809924155, 0.0181102772716804, 0.0332009036021612, 0.0291498591028205, 0.00639940473422184, 0.0225895045161622, 0.0101136400116731, 0.0263425240140077, 0.00435889894354067, 0.0731358813637816, 0.0127279220613579, 0.0471197057149837, 0.302157834819581, 0.0214941852602047, 0.0326233921333407, 0.0116833214455479, 0.0554204706893452, 0.130053450550149, 1.288200932641, 0.0353138985184506, 0.129266060068814, 0.0200997512422418, 0.0639262968470052, 0.0164032517101539, 0.0136793640203045, 0.00948306761699881, 0.0180193348537461, 0.00918753036924038, 0.0156296421675518), se = c(0.00933333333333333, 0.00446965074449646, 0.00688597608670453, 0.00283901391331568, 0.0064029499339869, 0.0135542121374378, 0.0103060315211184, 0.00241874763794291, 0.00798659591351123, 0.00357571171736681, 0.00931348868193715, 0.00251661147842358, 0.0258574388301924, 0.00636396103067893, 0.0192365393053024, 0.106828926994785, 0.00759934207678533, 0.0115341109013965, 0.00413067791046458, 0.0195940953204931, 0.0530940988560248, 0.455447807500643, 0.012485348556265, 0.0457024538259631, 0.00710633520177595, 0.0226013589983308, 0.00669659946871877, 0.00483638553053828, 0.0033527707092152, 0.00637079693377748, 0.00324828251322361, 0.00552591298209755 ), ci = c(0.0401580921443283, 0.0114896030154409, 0.0219142491554048, 0.00788236628321375, 0.0151405706956398, 0.0348422115168589, 0.0243698920725162, 0.00591846226021141, 0.0188852983847605, 0.00845521464359003, 0.0220229012042435, 0.0108281052473581, 0.0611431269419499, 0.0202529642690536, 0.0494490985187144, 0.252610271543504, 0.0179695885709161, 0.0272738383580029, 0.00976750116260314, 0.0463326729828588, 0.136482726098776, 1.07696293095078, 0.0295231579857332, 0.108069130674172, 0.0168038125580669, 0.0534437216064078, 0.0172141569548202, 0.0114362345155632, 0.00792804292904021, 0.0150645409315832, 0.0076809676067933, 0.0130667078496593)), row.names = c(NA, -32L), class = "data.frame") exp_funct <- function(y,a,b,t){y=a*exp(b*t)} ggplot(co2_data , aes(x = chamber_temp, y = co2_rate_u_m_h_g, colour = substrate, linetype = substrate, shape = substrate, fill = substrate), ymax=co2_rate_u_m_h_g+se, ymin=co2_rate_u_m_h_g-se) + geom_point(stat="identity", position = "dodge", width = 0.7) + geom_errorbar(aes(ymax=co2_rate_u_m_h_g+se, ymin=co2_rate_u_m_h_g-se), width=0.1, size=0.1, color="black") + facet_wrap(~depth) + stat_function(data = co2_data , aes(y= co2_rate_u_m_h_g, a = basal_respiration, b = slope, c = chamber_temp), fun = exp_funct(y =co2_rate_u_m_h_g, a = basal_respiration, b = slope, t = chamber_temp)) Error in exp_funct(y = co2_rate_u_m_h_g, a = basal_respiration, b = slope, : object 'basal_respiration' not found In addition: Warning message: Ignoring unknown parameters: width
Additionally to Allan's answer, a collegue figured out how to do it using nls library(tidyverse) install.packages("devtools") devtools::install_github("onofriAndreaPG/aomisc") df <- expand.grid(substrate = c("Calcareous", "Metapelite", "Peridotite"), depth = c("Mineral", "Organic")) mods <- purrr::map(split(df, 1:nrow(df)), function(x) nls(co2_rate_u_m_h_g ~ NLS.expoGrowth(chamber_temp, a, b), data = dplyr::filter(co2_data, substrate == x[[1]], depth == x[[2]]))) pred_dfs <- purrr::map(split(df, 1:nrow(df)), function(x) expand.grid(substrate = x[[1]], depth = x[[2]], chamber_temp = seq(from = 0, to = 35))) nls_pred <- function(x, y) { pred <- predict(x, newdata = y) y$pred <- pred return(y) } preds <- purrr::map2_dfr(mods, pred_dfs, nls_pred) ggplot(co2_data , aes(x = chamber_temp, y = co2_rate_u_m_h_g, linetype = substrate, shape = substrate), ymax=co2_rate_u_m_h_g+se, ymin=co2_rate_u_m_h_g-se) + geom_line(data = preds, aes(y = pred, x = chamber_temp, colour = substrate), linewidth = 1) + geom_errorbar(aes(ymax=co2_rate_u_m_h_g+se, ymin=co2_rate_u_m_h_g-se), width=0.1, size=0.1, color="black") + geom_point(aes(fill = substrate), size = 3) + scale_shape_manual(values = c(21, 22, 24)) + facet_wrap(~depth) + theme_bw() + theme(legend.position = "bottom")
You can't feed parameters into stat_function that way. In this case, I would probably just generate a little summary data frame inside a geom_line call: library(tidyverse) ggplot(co2_data , aes(x = chamber_temp, y = co2_rate_u_m_h_g, colour = substrate, linetype = substrate, shape = substrate, fill = substrate, ymax = co2_rate_u_m_h_g + se, ymin = co2_rate_u_m_h_g - se)) + geom_point(position = position_dodge(width = 0.7)) + geom_errorbar(width = 0.1, size = 0.1, color = 'black') + facet_wrap(~depth) + geom_line(data = . %>% group_by(substrate, depth) %>% summarize(chamber_temp = seq(0, 35, length.out = 100), basal_respiration = mean(basal_respiration), slope = mean(slope), se = mean(se), co2_rate_u_m_h_g = basal_respiration * exp(chamber_temp * slope)))
ggplot formula for a bar graph
I am looking to get a bar graph of medals in R. I have 3 distinct columns (gold, silver, bronze). The columns for gold medals has a total of 8, the silver has 10, and the bronze has 13. For the code, I started writing: ggplot(data, aes(x=?)) + geom_bar() I am not sure how to write all 3 gold medals on the function where it shows x=? Thanks
For plotting purposes, it is "easier" to work with long data instead of wide. Below I converted the data you mentioned in your comment to long and plotted the data as a grouped bar. library(tidyverse) # load data raw_data <- structure(list(Rank = c(1, 2, 3, 4, 5, 6), `Team/Noc` = c("United States of America", "People's Republic of China", "Japan", "Great Britain", "ROC", "Australia"), Gold = c(39, 38, 27, 22, 20, 17), Silver = c(41,32, 14, 21, 28, 7), Bronze = c(33, 18, 17, 22, 23, 22), Total = c(113, 88, 58, 65, 71, 46), `Rank by Total` = c(1, 2, 5, 4, 3, 6)), row.names = c(NA,-6L), class = c("tbl_df", "tbl", "data.frame")) # convert wide data to long long_data <- raw_data %>% pivot_longer(cols = -`Team/Noc`, names_to = 'Medal') %>% # convert wide data to long format filter(Medal %in% c("Gold", "Silver", "Bronze")) # only select medal columns # plot ggplot(long_data) + geom_col(aes(x = `Team/Noc`, y = value, fill = Medal), position = "dodge" # grouped bars ) Hope this gets you started!
How do I chart a new graph for multiple columns where there are 2 series for Y
I have a dataset df1. I would like to chart a line graph with Date as X and Chart1 and T1. I would like to loop it for all column where I would get the same graph when Y is Chart2 and T2 as well as when Y is Chart3 and T3. dput(df1) > structure(list(Date = c(1990, 1991, 1992, 1993, 1994), Chart1 = c(25, 34, 19, 7, 4), T1 = c(23.5, 23.5, 23.5, 23.5, 23.5), Chart2 = c(2, 4, 12, 9, 15), T2 = c(10, 10, 10, 10, 10), Chart3 = c(11, 9, 8, 6, 2), T3 = c(5, 5, 5, 5, 5)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame")) This is the chart that I would like to get for each column (Chart2,T2) and (Chart3,T3) etc. ggplot(df1, aes(x=Date)) + geom_line(aes(y=Chart1)) + geom_line(aes(y=T1), color="red")
You may separate out 'Chart' variables and 'T' variables and create list of plots. library(ggplot2) chart_cols <- grep('Chart', names(df1), value = TRUE) t_cols <- grep('T\\d+', names(df1), value = TRUE) list_plots <- Map(function(x, y) ggplot(df1, aes(x=Date)) + geom_line(aes(y=.data[[x]])) + geom_line(aes(y=.data[[y]]), color="red"), chart_cols, t_cols)
Faceted Boxplots
I have a boxplot with a group on the left (Baseline.RT) and a group on the right (TBPM.RT). I want to rename Baseline.RT to 1-back and TBPM.RT to TBPM. I also want each group to have a different square frame. I would also like to try black and white options to fill in the values for Neutral, Positive, and Negative. I have tried several options but did not get the result I wanted. Below is my code. Any help is welcome. DataFrame: data.frame( stringsAsFactors = FALSE, Participant = c(1, 2, 3, 4, 5, 6), Sex = c("m", "m", "m", "f", "f", "m"), Age = c(29, 21, 29, 22, 25, 31), Stress = c(14, 26, 11, 19, 15, 15), Dass21.total = c(6, 43, 4, 10, 12, 8), Dass21Ansie = c(0, 12, 1, 3, 2, 2), Dass.Depre = c(1, 11, 0, 1, 3, 0), Dass.Stress = c(5, 20, 3, 6, 7, 6), Valence = c(0, 1, 2, 0, 1, 2), Baseline.RT = c(1.17657473346937, 0.656485061072056,0.617504973518475,0.552112912223171, 0.587283706967395,0.569011248952529), TBPM.RT = c(1.16126499995575, 0.682658424923267,0.643632301167193,0.589782671563839, 0.705303832011063,0.691478784144668), TotalClockChecks = c(44, 97, 44, 93, 32, 90), TotalChecks5060 = c(13, 22, 17, 23, 10, 27), TotalClockResets = c(18, 20, 19, 19, 18, 19), Correct.Resets = c(16, 16, 18, 18, 12, 19), Before.55.Resets = c(0, 2, 0, 1, 0, 0), After.65.Resets = c(2, 2, 1, 0, 6, 0), MeanResetTime = c(63.0026438647087, 58.9040712799639,60.9928466685597,60.4603108544334, 65.859630879724,60.5522703813385), Accuracy.Baseline = c(0.987179487179487, 0.991489361702128,0.97907949790795,0.987234042553191,1, 0.987234042553191), Accuracy.TBPM = c(0.968619246861925, 0.972746331236897,0.989626556016598,0.972515856236786, 0.974736842105263,0.991786447638604), rau.Baseline = c(112.453264487601, 114.413187265486,109.508971532343,112.475825131896, 122.999999959683,112.475825131896), rau.TBPM = c(106.447535249234, 107.58519024216,113.516946707831,107.519541719961, 108.163803190644,114.564811317506) ) Plot: my44 %>% select(Participant, Valence, Baseline.RT,TBPM.RT) %>% #Select interest variables gather(Task,RT, -Valence, -Participant) %>% ggplot(., aes(factor(Valence), RT)) + #plot geom_boxplot() + facet_wrap(~ Task) + scale_x_discrete(name = element_blank(), labels=c("0" = "Neutral", "1" = "Positive", "2" = "Negative")) + scale_fill_discrete(name="Valence", breaks=c("0", "1", "2"), labels=c("Neutral", "Positive", "Negative")) The obtained results: Created on 2020-09-02 by the reprex package (v0.3.0)
It sounds like you're looking for something like this (although your question's input data doesn't produce the values displayed in your plot, and you seem to have a default theme set somewhere). Your fill colours can be chosen by scale_fill_manual, but you need to map the Valence variable to the fill scale if you want the different boxes to have different colours. If you want a frame around each facet, theme_bw does this by default, or you can use theme(panel.border = element_rect(colour = "black")). To re-name facets, I would normally just re-name the faceting variables to the desired names in the input, but here I have shown an alternative method using the labeller parameter in facet_wrap. my44 %>% select(Participant, Valence, Baseline.RT,TBPM.RT) %>% #Select interest variables gather(Task,RT, -Valence, -Participant) %>% ggplot(., aes(factor(Valence), RT)) + geom_boxplot(aes(fill = factor(Valence))) + facet_wrap(~ Task, labeller = function(x) data.frame(Task = c("1-back", "TBPM"))) + scale_x_discrete(name = element_blank(), labels=c("0" = "Neutral", "1" = "Positive", "2" = "Negative")) + scale_fill_manual(name="Valence", breaks=c("0", "1", "2"), labels=c("Neutral", "Positive","Negative"), values = c("gray50", "gray75", "gray95")) + theme_bw() + theme(legend.position = "none", strip.background = element_blank())
can't add labels to my graph
I have this graph: I just need to add labels to each colored line. I need to add to the blue one Forecast Sales and for the red one Historical Sales. I tried to adapt these examples here but I have much error. Also, I can not plot the graph above just by using this code: to make it reproductible : dput(df1) structure(list(Semaine = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31), M = c(5649.96284329564, 7400.19639744335, 6948.61488673139, 5043.28209277238, 7171.29719525351, 7151.04746494067, 5492.96601941748, 6796.1160130719, 5532.95496473142, 7371.33061889251, 5462.73861171367, 7156.01570964247, 5558.63194819212, 9329.49289405685, 5770.02903225806, 7348.68497576737, 5261.26655896607, 8536.11304909561, 7463.97630586968, 6133.49774339136, 7252.69089929995, 6258.54674403611, 8167.67766497462, 5644.66612816371, 7512.5169628433, 5407.84275713516, 7795.63220247711, 5596.75282714055, 7264.37264404954, 5516.98492191707, 8188.80776699029 > dput(df2) structure(list(Semaine = c(32, 33.2, 34.4, 35.6, 36.8, 38), M = c(5820.32304669441, 6296.32038834951, 7313.24757281553, 7589.714214588, 8992.35922330097, 9664.95469255663)), .Names = c("Semaine", "M"), row.names = c(NA, -6L), class = "data.frame") ggplot() + geom_line(data=df1, aes(x = Semaine, y = M),color = "red") + stat_smooth(data=df2, aes(x = Semaine, y = M),color = "blue")+ scale_x_continuous(breaks = seq(0,40,1)) Thank you!
cols <- c("A"="red", "B"="blue") ggplot() + geom_line(data=df1, aes(x = Semaine, y = M,color = "A")) + stat_smooth(data=df2, aes(x = Semaine, y = M,color = "B"), method = 'loess')+ scale_x_continuous(breaks = seq(0,40,1)) + scale_color_manual(name="Title", values=cols)