Make ggplot connect datapoints in a scatterplot chronologically - r

This seems very simple but for some reason I can't make it work.
I have a dataset with 3 variables. The first variable is a measurement which is taken several times per day across two months (it can take the values 1, 2, 3, 4, 5 and 6 - these are not groups, it is values that have been measured). The second variable are the dates. The third variable are the times the measurement was taken. I want to plot how this measurement changes across time so I need the datapoints to be connected chronologically.
Things I have tried:
I have tried to plot just using date by making sure it is a date format and it is ordered correctly and then specified + geom_path() which should tell R I want it to go row by row connecting
DF$Date <- as.Date(DF$Date)
DF <- DF[order(DF$Date),]
ggplot(DF, aes(x = Date, y = Measurement)) +
geom_line(linewidth=1, colour="green") +
geom_path()
I created a DateTime variable:
DF$DateTime <- as.POSIXct(paste(DF$Date, DF$Time, format="%y/%m/%d %H:%M:%S"))
ggplot(DF, aes(x = DateTime, y = Measurement)) +
geom_line(linewidth=1, colour="green")
In both cases R just connects all the response of value 1 to each other, all responses of value 2 to each other and so on. And does not do it chronologically.
Thank you!
structure(list(Measurement = c("1", "1", "1", "1", "2", "1", "1",
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
"1", "1", "1", "2", "2", "1", "2", "2", "2", "1", "2", "1", "1",
"1", "2", "1", "2", "2", "2", "1", "2", "3", "2", "2", "4", "3",
"2", "2", "2", "2", "3", "3", "3", "3", "3", "4", "3", "3", "2",
"2", "4", "4", "3", "1", "1", "2", "1", "1", "4", "3", "3", "3",
"3", "3", "3", "4", "3", "3", "3", "3", "3", "3", "3", "3", "3",
"3", "5", "4", "5", "3", "5", "5", "5", "4", "4", "4", "4", "4",
"4", "3", "5", "4", "4", "4", "4", "4", "4", "4", "4", "4", "5",
"4", "4", "5", "5", "5", "5", "4", "3", "4", "4", "4", "4", "3",
"4", "3", "4", "4", "4", "4", "3", "5", "4", "4", "5", "4", "4",
"4", "4", "4", "4", "4", "4", "3", "3", "3", "3", "4", "3", "4",
"3", "3", "2", "3", "4", "4", "4", "4", "4", "5", "4", "4", "4",
"4", "3", "4", "5", "4", "3", "4", "4", "4", "4", "4", "3", "4",
"1", "4", "4", "3", "4", "4", "3", "4", "4", "4", "3", "4", "4",
"4", "4", "6", "4", "4", "4", "4", "3", "3", "4", "4", "3", "3",
"4", "3", "3", "3", "4", "5", "4", "4", "4", "4", "1", "3", "4",
"3", "4", "4", "4", "1", "3", "4", "5", "5", "5", "5", "5", "5",
"5", "4", "4", "4", "4", "4", "5", "4", "3", "4", "4", "4", "4",
"5", "4", "4", "3", "4", "4", "4", "4", "4", "5", "4", "4", "4",
"4", "4", "3", "5", "4", "4", "4", "4", "3", "3", "3", "4", "4",
"3", "4", "4", "5"), Date = structure(c(19333, 19333,
19334, 19334, 19334, 19334, 19334, 19334, 19334, 19335, 19335,
19335, 19335, 19335, 19335, 19335, 19335, 19336, 19336, 19336,
19336, 19336, 19336, 19337, 19337, 19337, 19337, 19337, 19337,
19338, 19338, 19338, 19338, 19338, 19338, 19338, 19338, 19338,
19339, 19339, 19339, 19339, 19339, 19339, 19339, 19339, 19339,
19340, 19340, 19340, 19340, 19341, 19341, 19341, 19341, 19342,
19342, 19342, 19342, 19342, 19342, 19342, 19343, 19343, 19343,
19343, 19343, 19344, 19344, 19344, 19344, 19344, 19345, 19345,
19345, 19345, 19346, 19346, 19346, 19346, 19347, 19347, 19347,
19347, 19348, 19348, 19349, 19350, 19350, 19350, 19350, 19350,
19350, 19351, 19351, 19351, 19352, 19353, 19353, 19353, 19353,
19353, 19354, 19355, 19355, 19355, 19356, 19356, 19356, 19356,
19357, 19357, 19357, 19357, 19358, 19358, 19358, 19358, 19359,
19359, 19359, 19359, 19360, 19360, 19360, 19360, 19360, 19361,
19361, 19362, 19362, 19362, 19362, 19363, 19363, 19363, 19363,
19364, 19364, 19364, 19364, 19364, 19365, 19365, 19365, 19365,
19365, 19366, 19366, 19366, 19366, 19366, 19367, 19367, 19367,
19367, 19367, 19368, 19368, 19368, 19368, 19369, 19369, 19369,
19370, 19370, 19370, 19371, 19371, 19371, 19372, 19372, 19372,
19372, 19372, 19373, 19373, 19373, 19373, 19374, 19374, 19374,
19374, 19374, 19374, 19375, 19375, 19375, 19375, 19376, 19376,
19376, 19377, 19377, 19377, 19377, 19377, 19378, 19378, 19378,
19378, 19379, 19379, 19379, 19379, 19379, 19380, 19380, 19380,
19380, 19380, 19381, 19381, 19381, 19381, 19382, 19382, 19382,
19382, 19383, 19383, 19385, 19385, 19385, 19385, 19385, 19385,
19385, 19385, 19385, 19385, 19386, 19386, 19386, 19386, 19386,
19387, 19387, 19387, 19387, 19387, 19387, 19387, 19387, 19387,
19388, 19388, 19388, 19388, 19388, 19388, 19388, 19388, 19388,
19389, 19389, 19389, 19389, 19389, 19389, 19389, 19389, 19390,
19390, 19390, 19390, 19390, 19390, 19390, 19390, 19390), class = "Date"),
Time = structure(c(43810, 44174, 49104, 49343, 50921,
54029, 59443, 65767, 70544, 40647, 40731, 43219, 50506, 54044,
58687, 68571, 71016, 36049, 38921, 44148, 55413, 66503, 70310,
34796, 48468, 48770, 56701, 67069, 73131, 32103, 37937, 43270,
43941, 49507, 57796, 59420, 65187, 70669, 28787, 33612, 38807,
43900, 49607, 54026, 60525, 65861, 76855, 29833, 43197, 45349,
67928, 34018, 44887, 54024, 65491, 34687, 45029, 45029, 45096,
45096, 56881, 70503, 30726, 49625, 54871, 76945, 76990, 30348,
51899, 58286, 65893, 76301, 34075, 54033, 54075, 66322, 34158,
47973, 69070, 69113, 29971, 43838, 43891, 68344, 58512, 64840,
74134, 42286, 48249, 53712, 75669, 75669, 75669, 34484, 67922,
67922, 63298, 30761, 30814, 52835, 67936, 78132, 69679, 44485,
61309, 65893, 32443, 46595, 55031, 65701, 40995, 43257, 78737,
78783, 35103, 58260, 65353, 78583, 36651, 44588, 53857, 74257,
34262, 44172, 50954, 56508, 68744, 32577, 54241, 32233, 45405,
59002, 68596, 33529, 44235, 56676, 65104, 35378, 43263, 59195,
70423, 76305, 34704, 40350, 43769, 54069, 65163, 32335, 43220,
52463, 64829, 64883, 33312, 47326, 56974, 78210, 78249, 37710,
47664, 51668, 67281, 39815, 57103, 67451, 52368, 54111, 66853,
45038, 45079, 64861, 35856, 45970, 54136, 54174, 67102, 32497,
49309, 56959, 68312, 33326, 44280, 53945, 54763, 65275, 65313,
32958, 52099, 57512, 65378, 27223, 58171, 64993, 32862, 44507,
44547, 54631, 76109, 33983, 49720, 58810, 66231, 29886, 53075,
54592, 64904, 64942, 29982, 40303, 43288, 54319, 65762, 28881,
36993, 44716, 65239, 34587, 43395, 64886, 66650, 41670, 53480,
29252, 38412, 38477, 38477, 44963, 48648, 56521, 59572, 65410,
70232, 32517, 38681, 43273, 50715, 74179, 33337, 39419, 39419,
40341, 49560, 59123, 60091, 65164, 70217, 37318, 37822, 43242,
43287, 49346, 55187, 59908, 64815, 72710, 32553, 33678, 37864,
43283, 54029, 59412, 68693, 78965, 34730, 38193, 43936, 51483,
54039, 54134, 59417, 66687, 72937), class = c("hms", "difftime"
), units = "secs"), DateTime = structure(c(1670415010, 1670415374,
1670506704, 1670506943, 1670508521, 1670511629, 1670517043,
1670523367, 1670528144, 1670584647, 1670584731, 1670587219,
1670594506, 1670598044, 1670602687, 1670612571, 1670615016,
1670666449, 1670669321, 1670674548, 1670685813, 1670696903,
1670700710, 1670751596, 1670765268, 1670765570, 1670773501,
1670783869, 1670789931, 1670835303, 1670841137, 1670846470,
1670847141, 1670852707, 1670860996, 1670862620, 1670868387,
1670873869, 1670918387, 1670923212, 1670928407, 1670933500,
1670939207, 1670943626, 1670950125, 1670955461, 1670966455,
1671005833, 1671019197, 1671021349, 1671043928, 1671096418,
1671107287, 1671116424, 1671127891, 1671183487, 1671193829,
1671193829, 1671193896, 1671193896, 1671205681, 1671219303,
1671265926, 1671284825, 1671290071, 1671312145, 1671312190,
1671351948, 1671373499, 1671379886, 1671387493, 1671397901,
1671442075, 1671462033, 1671462075, 1671474322, 1671528558,
1671542373, 1671563470, 1671563513, 1671610771, 1671624638,
1671624691, 1671649144, 1671725712, 1671732040, 1671827734,
1671882286, 1671888249, 1671893712, 1671915669, 1671915669,
1671915669, 1671960884, 1671994322, 1671994322, 1672076098,
1672129961, 1672130014, 1672152035, 1672167136, 1672177332,
1672255279, 1672316485, 1672333309, 1672337893, 1672390843,
1672404995, 1672413431, 1672424101, 1672485795, 1672488057,
1672523537, 1672523583, 1672566303, 1672589460, 1672596553,
1672609783, 1672654251, 1672662188, 1672671457, 1672691857,
1672738262, 1672748172, 1672754954, 1672760508, 1672772744,
1672822977, 1672844641, 1672909033, 1672922205, 1672935802,
1672945396, 1672996729, 1673007435, 1673019876, 1673028304,
1673084978, 1673092863, 1673108795, 1673120023, 1673125905,
1673170704, 1673176350, 1673179769, 1673190069, 1673201163,
1673254735, 1673265620, 1673274863, 1673287229, 1673287283,
1673342112, 1673356126, 1673365774, 1673387010, 1673387049,
1673432910, 1673442864, 1673446868, 1673462481, 1673521415,
1673538703, 1673549051, 1673620368, 1673622111, 1673634853,
1673699438, 1673699479, 1673719261, 1673776656, 1673786770,
1673794936, 1673794974, 1673807902, 1673859697, 1673876509,
1673884159, 1673895512, 1673946926, 1673957880, 1673967545,
1673968363, 1673978875, 1673978913, 1674032958, 1674052099,
1674057512, 1674065378, 1674113623, 1674144571, 1674151393,
1674205662, 1674217307, 1674217347, 1674227431, 1674248909,
1674293183, 1674308920, 1674318010, 1674325431, 1674375486,
1674398675, 1674400192, 1674410504, 1674410542, 1674461982,
1674472303, 1674475288, 1674486319, 1674497762, 1674547281,
1674555393, 1674563116, 1674583639, 1674639387, 1674648195,
1674669686, 1674671450, 1674732870, 1674744680, 1674893252,
1674902412, 1674902477, 1674902477, 1674908963, 1674912648,
1674920521, 1674923572, 1674929410, 1674934232, 1674982917,
1674989081, 1674993673, 1675001115, 1675024579, 1675070137,
1675076219, 1675076219, 1675077141, 1675086360, 1675095923,
1675096891, 1675101964, 1675107017, 1675160518, 1675161022,
1675166442, 1675166487, 1675172546, 1675178387, 1675183108,
1675188015, 1675195910, 1675242153, 1675243278, 1675247464,
1675252883, 1675263629, 1675269012, 1675278293, 1675288565,
1675330730, 1675334193, 1675339936, 1675347483, 1675350039,
1675350134, 1675355417, 1675362687, 1675368937), class = c("POSIXct",
"POSIXt"), tzone = "")), row.names = c(NA, -271L), class = c("tbl_df",
"tbl", "data.frame"))

You can connect the points chronologically by specifying group = 1. Additionally, you can use scale_x_datetime to control the breaks on your x-axis. For the way the x-axis is labelled, see this r-bloggers post for more information about date formats.
I've included one example that best recreates what you were trying to do and another example where the points are colored by Measurement.
library(tidyverse)
DF$Date <- as.Date(DF$Date)
DF <- DF[order(DF$Date),]
DF$DateTime <- as.POSIXct(paste(DF$Date, DF$Time, format = "%y/%m/%d %H:%M:%S"))
# what I think you want
ggplot(DF, aes(x = DateTime, y = Measurement, group = 1)) +
geom_line(linewidth = 1, color = "green") +
geom_point() +
scale_x_datetime(date_breaks = "10 days", date_labels = "%b %d %y")
# another option
ggplot(DF, aes(x = DateTime, y = Measurement, color = Measurement, group = 1)) +
geom_line(linewidth = 1, color = "gray") +
geom_point() +
scale_x_datetime(date_breaks = "10 days", date_labels = "%b %d %y")

Related

Linear mixed model estimator output incorrect

I want to perform a linear mixed model with a fixed effect for time plus a random intercept & slope for time per subject. I do this with the following code:
lme.rik<-lmer(formula = AI ~ Time + (1+Time|ID), data=df6, control = lmerControl(check.nobs.vs.nRE = "ignore"))
I use the control = lmerControl(check.nobs.vs.nRE = "ignore" because there is probably something wrong with my Time variable. Nonetheless, the mode gives some output where it shows as fixed effects some sort of dummy variable for the number of weeks (i.e., time variable). So first the intercept, then Time[2], Time[..], Time[7]. Only excluding Time[1]. Same applies for the random effects. Is there a way to change the Time variable so that it will only include 1 fixed and 1 random estimate for estimate for Time?
The following output is my dataset:
structure(list(ID = c("ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID10", "ID10", "ID10", "ID10", "ID10", "ID10", "ID10",
"ID11", "ID11", "ID11", "ID11", "ID11", "ID11", "ID11", "ID12",
"ID12", "ID12", "ID12", "ID12", "ID12", "ID12", "ID13", "ID13",
"ID13", "ID13", "ID13", "ID13", "ID13", "ID14", "ID14", "ID14",
"ID14", "ID14"), Time = c("1", "2", "3", "4", "5", "6", "7",
"1", "2", "3", "4", "5", "6", "7", "1", "2", "3", "4", "5", "6",
"7", "1", "2", "3", "4", "5", "6", "7", "1", "2", "3", "4", "5",
"6", "7", "1", "2", "3", "4", "5"), 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, NA, NA, 0.520204263001733,
0.194175420670027, 0.507417309543222, 0.1934679395598, 0.0831932654836405,
0.115391861884329, 0.141420940969022, 0.361215896677733, 0.256393554215028,
0.429431082438377, NA, NA, NA, NA, 0.239250372076152, 0.219099984707727,
NA, 0.289692898163938, 0.287732972580083), 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, NA, NA, 0.270612475245176,
0.0377040939923819, 0.257472326024082, 0.0374298436375145, 0.00692111942183149,
0.0133152817891321, 0.0199998825445637, 0.130476924012699, 0.0657376546430145,
0.184411054564196, NA, NA, NA, NA, 0.0572407405385771, 0.0480048032989263,
NA, 0.0839219752466215, 0.0827902635097706), 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, NA, NA, 0.238888888888889, 0.263888888888889,
0.247916666666667, 0.315277777777778, 0.298611111111111, 0.173611111111111,
0.185416666666667, 0.45625, 0.239583333333333, 0.335416666666667,
NA, NA, NA, NA, 0.36875, 0.251388888888889, NA, 0.266666666666667,
0.309722222222222)), row.names = c(NA, -40L), class = c("tbl_df",
"tbl", "data.frame"))

Create (many) columns conditional on similarly named columns

I want to create a new column that take the value of one of two similarly named columns, depending on a third column. There are many such columns to create. Here's my data.
dt <- structure(list(malvol_left_1_w1 = c("1", "1", "4", "3", "4",
"4", "1", "4", "4", "3", "1", "4", "4", "3", "4", "4", "5", "2",
"4", "2"), malvol_left_2_w1 = c("1", "1", "4", "3", "4", "4",
"1", "3", "4", "2", "2", "2", "4", "1", "5", "4", "5", "2", "4",
"2"), malvol_right_1_w1 = c("1", "1", "4", "3", "4", "4", "1",
"3", "4", "2", "1", "4", "4", "5", "5", "4", "2", "6", "4", "1"
), malvol_right_2_w1 = c("1", "1", "4", "3", "4", "4", "1", "3",
"4", "2", "1", "2", "4", "5", "5", "4", "5", "5", "4", "5"),
malvol_left_1_w2 = c("1", "1", "3", "3", "4", "4", "1", "5",
"4", "4", "4", "2", "1", "4", "5", "4", "3", "2", "4", "4"
), malvol_left_2_w2 = c("1", "1", "3", "3", "4", "4", "7",
"5", "4", "2", "3", "1", "1", "4", "4", "4", "3", "4", "4",
"4"), malvol_right_1_w2 = c("1", "3", "3", "3", "4", "4",
"1", "4", "4", "3", "2", "2", "4", "1", "4", "4", "5", "5",
"4", "4"), malvol_right_2_w2 = c("1", "2", "3", "3", "4",
"4", "1", "2", "4", "2", "3", "2", "4", "1", "4", "4", "5",
"4", "4", "3"), leftright_w1 = c("right", "right", "left",
"right", "right", "right", "left", "right", "right", "left",
"left", "left", "left", "right", "left", "left", "right",
"right", "right", "left"), leftright_w2 = c("right", "right",
"left", "left", "right", "left", "left", "right", "right",
"left", "left", "left", "left", "right", "left", "left",
"right", "right", "left", "left")), class = "data.frame", row.names = c("12",
"15", "69", "77", "95", "96", "112", "122", "150", "163", "184",
"216", "221", "226", "240", "298", "305", "354", "370", "379"
))
Now I can do this in dplyr like:
dt <- dt %>%
mutate(
malvol_1_w1 = case_when(
leftright_w1 == "left" ~ malvol_right_1_w1,
leftright_w1 == "right" ~ malvol_left_1_w1),
malvol_2_w1 = case_when(
leftright_w1 == "left" ~ malvol_right_2_w1,
leftright_w1 == "right" ~ malvol_left_2_w1),
malvol_1_w2 = case_when(
leftright_w2 == "left" ~ malvol_right_1_w2,
leftright_w2 == "right" ~ malvol_left_1_w2),
malvol_2_w2 = case_when(
leftright_w2 == "left" ~ malvol_right_2_w2,
leftright_w2 == "right" ~ malvol_left_2_w2))
However, it's not really a feasible solution, because there will be more of both numbers defining a variable (e.g. both malvol_3_w1 and malvol_1_w3 will need to be created).
One solution is to this with a loop:
for (wave in 1:2) {
for (var in 1:2) {
dt[, paste0("malvol_", var, "_w", wave)] <- dt[, paste0("malvol_right_", var, "_w", wave)]
dt[dt[[paste0("leftright_w", wave)]] == "right", paste0("malvol_", var, "_w", wave)] <-
dt[dt[[paste0("leftright_w", wave)]] == "right", paste0("malvol_left_", var, "_w", wave)]
}
}
However, what is a tidyverse solution?
UPDATE:
I came up with a tidyverse solution myself, however, not every elegant. Still looking for more canonical solutions.
dt <- dt %>%
mutate(
malvol_1_w1 = NA, malvol_2_w1 = NA,
malvol_1_w2 = NA, malvol_2_w2 = NA) %>%
mutate(
across(matches("malvol_\\d"),
~ case_when(
eval(parse(text = paste0("leftright_", str_extract(cur_column(), "w.")))) == "left" ~
eval(parse(text = paste0(str_split(cur_column(), "_\\d", simplify = T)[1],
"_right", str_split(cur_column(), "malvol", simplify = T)[2]))),
eval(parse(text = paste0("leftright_", str_extract(cur_column(), "w.")))) == "right" ~
eval(parse(text = paste0(str_split(cur_column(), "_\\d", simplify = T)[1],
"_left", str_split(cur_column(), "malvol", simplify = T)[2]))))))
What makes your problem difficult is that a lot of information is hidden in variable names rather than data cells. Hence, you need some steps to transform your data into "tidy" format. In the code below, the crucial part is (1) to turn the variables [malvol]_[lr]_[num]_[w] into four separate columns malvol, lr, num, w (all prefixed with m_), and (2) from the variables leftright_[w] extract variable w (prefixed with l_) using the functions pivot_longer and than separate.
# Just adding a row_id to your data, for later joining
dt <- dt %>% mutate(id = row_number())
df <- dt %>%
# Tidy the column "malvol"
pivot_longer(cols = starts_with('malvol'), names_to = "m_var", values_to = "m_val") %>%
separate(m_var, into = c("m_malvol", "m_lr", "m_num", "m_w")) %>%
# They the column "leftright"
pivot_longer(cols = starts_with('leftright'), names_to = 'l_var', values_to = 'l_lr') %>%
separate(l_var, into = c(NA, "l_w")) %>%
# Implement the logic
filter(l_w == m_w) %>%
filter(l_lr != m_lr) %>%
# Pivot into original wide format
select(-c(l_w, l_lr, m_lr)) %>%
pivot_wider(names_from = c(m_malvol, m_num, m_w), values_from = m_val)
# Merging back results to original data
dt <- dt %>% mutate(id = row_number()) %>% inner_join(df, by="id")
Although I pivoted the data back into your desired format in the end (to check whether results are in line with your desired results), I would suggest you leave the data in the long format, which is "tidy" and more easy to work with, compared to your "wide" format. So maybe skip the last pivot_wider operation.

Recode a factor variable, dropping N/A

I have a factor variable with 14 levels, which I'm trying to into collapse into only 3 levels. It contains two N/A which I also wanna remove.
My code looks like this:
job <- fct_collapse(E$occupation, other = c("7","9", "10", "13" "14"), 1 = c("1", "2", "3", "12"), 2 = c("4", "5", "6", "8", "11"))
However it just gives me tons of error. Can anyone help here me here?
We could also this with a named list
library(forcats)
lst1 <- setNames(list(as.character(c(7, 9, 10, 13, 14)),
as.character(c(1, 2, 3, 12)), as.character(c(4, 5, 6, 8, 11))), c('other', 1, 2))
fct_collapse(df$occupation, !!!lst1)
data
df <- structure(list(occupation = c("1", "3", "5", "7", "9", "10",
"12", "14", "13", "4", "7", "6", "5")), class = "data.frame", row.names = c(NA,
-13L))
For numbers try using backquotes in fct_collapse.
job <- forcats::fct_collapse(df$occupation,
other = c("7","9", "10", "13", "14"),
`1` = c("1", "2", "3", "12"),
`2` = c("4", "5", "6", "8", "11"))

Problem with Piping for revalue in R Studio

I would like to revalue 13 different variables. They all have character as levels right now and are supposed to be changed to values.
Individually it would work to use
x$eins <- revalue(x$eins, c("Nie Thema" = "1",
"Selten Thema" = "2",
"Manchmal Thema" = "3",
"Häufig Thema" = "4",
"Sehr häufig Thema" = "5",
"Fast immer Thema" = "6"))
With the piping, I guess it would look something like this
x %>%
dplyr::select(., eins:dreizehn) %>%
revalue(., c("Nie Thema" = "1",
"Selten Thema" = "2",
"Manchmal Thema" = "3",
"Häufig Thema" = "4",
"Sehr häufig Thema" = "5",
"Fast immer Thema" = "6"))
With this, I get the warning message from revalue, that x is not a factor or a character vector.
What am I doing wrong?
Thanks in advance.
Use across to apply a function for multiple columns.
library(dplyr)
x <- x %>%
dplyr::mutate(across(eins:dreizehn, ~revalue(., c("Nie Thema" = "1",
"Selten Thema" = "2",
"Manchmal Thema" = "3",
"Häufig Thema" = "4",
"Sehr häufig Thema" = "5",
"Fast immer Thema" = "6"))))

Two Color Scales for geom_line in ggplot2

I have a chart (code to replicate will be below) that has two lines (and points) of data that need to be color coded, then three sets of confidence intervals (lines) which need to have their own color coding.
Unfortunately, ggplot sees the two calls to geom_line() and fits them all in the same scale.
Is there a way to have the central lines and dots have one scale (and legend entry) while the outer lines have a seperate scale (and legend entry)?
I've seen (complex) answers like ggplot2: Multiple color scales or shift colors systematically on different layers? but that relies on the old proto system which I believe has been phased out by now(?).
Thanks for any help.
Code to produce data and graphs. Sorry for the length:
exShapedMayGroup <- structure(list(Date = structure(c(14730, 14730, 14730, 14731,
14731, 14731, 14734, 14734, 14734, 14735, 14735, 14735, 14736,
14736, 14736, 14737, 14737, 14737, 14740, 14740, 14740, 14741,
14741, 14741, 14742, 14742, 14742, 14743, 14743, 14743, 14744,
14744, 14744, 14745, 14745, 14745, 14746, 14746, 14746, 14748,
14748, 14748, 14749, 14749, 14749, 14750, 14750, 14750, 14750,
14750, 14750, 14751, 14751, 14751, 14752, 14752, 14752, 14752,
14752, 14752, 14754, 14754, 14754, 14756, 14756, 14756, 14757,
14757, 14757, 14758, 14758, 14758, 14758, 14758, 14758, 14759,
14759, 14759, 14760, 14760, 14760), class = "Date"), Score = c(0.028,
0.028, 0.028, 0.03289, 0.03289, 0.03289, 0.034512, 0.034512,
0.034512, 0.0373496, 0.0373496, 0.0373496, 0.03201968, 0.03201968,
0.03201968, 0.040805744, 0.040805744, 0.040805744, 0.0344045952,
0.0344045952, 0.0344045952, 0.04017367616, 0.04017367616, 0.04017367616,
0.035998940928, 0.035998940928, 0.035998940928, 0.0342191527424,
0.0342191527424, 0.0342191527424, 0.09799532219392, 0.09799532219392,
0.09799532219392, 0.122746257755136, 0.122746257755136, 0.122746257755136,
0.0999570062041088, 0.0999570062041088, 0.0999570062041088, 0.0950656049632871,
0.0950656049632871, 0.0950656049632871, 0.0837224839706296, 0.0837224839706296,
0.0837224839706296, 0.00418, 0.00418, 0.00418, 0.0806379871765037,
0.0806379871765037, 0.0806379871765037, 0.009624, 0.009624, 0.009624,
0.0099792, 0.0099792, 0.0099792, 0.090740389741203, 0.090740389741203,
0.090740389741203, 0.0905523117929624, 0.0905523117929624, 0.0905523117929624,
0.0761218494343699, 0.0761218494343699, 0.0761218494343699, 0.0707874795474959,
0.0707874795474959, 0.0707874795474959, 0.02132336, 0.02132336,
0.02132336, 0.0636099836379967, 0.0636099836379967, 0.0636099836379967,
0.0550479869103974, 0.0550479869103974, 0.0550479869103974, 0.0466883895283179,
0.0466883895283179, 0.0466883895283179), Right = c("1", "2",
"3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3",
"1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1",
"2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2",
"3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3",
"1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1",
"2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2",
"3"), .id = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0",
"0", "0", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0", "0", "0",
"0", "0", "0", "0", "0", "0"), Lower = c(0.0452301816389807,
0.0299531343622987, 0.0146760870856168, 0.0409430625769167, 0.0213788962381707,
0.00181472989942479, 0.0386359600820249, 0.0167646912483872,
-0.00510657758525054, 0.037279363974053, 0.0140514990324434,
-0.00917636590916623, 0.0364512577706185, 0.0123952866255743,
-0.0116606845194698, 0.0359359120595814, 0.0113645952035002,
-0.0132067216525811, 0.0356116886483614, 0.0107161483810601,
-0.0141793918862411, 0.035406383399575, 0.0103055378834873, -0.0147953076326005,
0.0352758647295475, 0.0100445005434323, -0.0151868636426829,
0.0351926859362388, 0.00987814295681498, -0.0154364000226088,
0.035139594640892, 0.00977196036612139, -0.0155956739086492,
0.0351056744462797, 0.00970411997689682, -0.0156974344924861,
0.0350839892725913, 0.00966074962952, -0.0157624900135513, 0.0350701204632195,
0.00963301201077625, -0.0158040964416669, 0.035061248392137,
0.00961526786861143, -0.0158307126549142, NA, NA, NA, 0.0350555718896789,
0.00960391486369513, -0.0158477421622886, NA, NA, NA, NA, NA,
NA, 0.0350519395924259, 0.00959665026918906, -0.0158586390540477,
0.0350496151941651, 0.00959200147266757, -0.01586561224883, 0.0350481276906492,
0.00958902646563569, -0.0158700747593778, 0.035047175734008,
0.00958712255235328, -0.0158729306293014, NA, NA, NA, 0.0350465665004368,
0.00958590408521094, -0.0158747583300149, 0.0350461765986017,
0.00958512428154069, -0.0158759280355203, 0.0350459270645606,
0.00958462521345864, -0.0158766766376434), Upper = c(0.0757842761923446,
0.0910613234690266, 0.106338370745709, 0.0800713952544086, 0.0996355615931546,
0.119199727931901, 0.0823784977493004, 0.104249766582938, 0.126121035416576,
0.0837350938572723, 0.106962958798882, 0.130190823740492, 0.0845632000607068,
0.108619171205751, 0.132675142350795, 0.0850785457717439, 0.109649862627825,
0.134221179483906, 0.0854027691829639, 0.110298309450265, 0.135193849717566,
0.0856080744317504, 0.110708919947838, 0.135809765463926, 0.0857385931017778,
0.110969957287893, 0.136201321474008, 0.0858217718950865, 0.11113631487451,
0.136450857853934, 0.0858748631904333, 0.111242497465204, 0.136610131739975,
0.0859087833850456, 0.111310337854428, 0.136711892323811, 0.085930468558734,
0.111353708201805, 0.136776947844877, 0.0859443373681059, 0.111381445820549,
0.136818554272992, 0.0859532094391883, 0.111399189962714, 0.136845170486239,
NA, NA, NA, 0.0859588859416464, 0.11141054296763, 0.136862199993614,
NA, NA, NA, NA, NA, NA, 0.0859625182388994, 0.111417807562136,
0.136873096885373, 0.0859648426371602, 0.111422456358658, 0.136880070080155,
0.0859663301406761, 0.11142543136569, 0.136884532590703, 0.0859672820973173,
0.111427335278972, 0.136887388460627, NA, NA, NA, 0.0859678913308885,
0.111428553746114, 0.13688921616134, 0.0859682812327236, 0.111429333549785,
0.136890385866846, 0.0859685307667647, 0.111429832617867, 0.136891134468969
)), .Names = c("Date", "Score", "Right", ".id", "Lower", "Upper"
), row.names = c(NA, 81L), class = "data.frame")
ggplot(exShapedMayGroup, aes_string(x="Date", y="Score")) + geom_line(aes_string(group=".id", colour=".id")) +
geom_point(aes_string(colour=".id")) + geom_line(aes_string(y="Lower", colour="Right")) +
geom_line(aes_string(y="Upper", colour="Right")) + scale_color_discrete(name="Limits")
P.S. Only using aes_string because this is called in a function which allows the user to input columns as a character.
The ggplot2 way to do this is to not use geom_line for your confidence bands. There's a geom built specifically for that: geom_ribbon.
ggplot(exShapedMayGroup, aes(x=Date, y=Score)) +
geom_ribbon(aes(ymin = Lower,ymax = Upper,fill = Right,group = Right),alpha = 0.25) +
geom_line(aes(group= .id, colour= .id)) +
geom_point(aes(colour = Right)) +
scale_color_discrete(name="Limits")
Obviously, I can't be sure that you'll be happy with how this looks, but this is generally how you approach this kind of graph in ggplot2. Note that I removed the aes_string uses in your code.
Unfortunately Hadley confirmed this is not possible. So I decided to use linetype instead (and not have anything distinguishing the points). This made everyone happy.
ggplot(exShapedMayGroup, aes_string(x="Date", y="Score")) +
geom_line(aes_string(group=".id", linetype=".id")) +
geom_point() +
geom_line(aes_string(y="Lower", colour="Right")) +
geom_line(aes_string(y="Upper", colour="Right")) +
scale_linetype_discrete(name="Group") + scale_color_discrete(name="Limits")
This is much simpler now, thanks to the Elio Campitelli's ggnewscale package
#install.packages("ggnewscale")
library(ggnewscale)
ggplot(exShapedMayGroup, aes(x=Date, y=Score)) +
geom_line(aes(color=.id)) +
geom_point(aes(color = .id)) +
scale_color_brewer(palette = "Dark2") +
ggnewscale::new_scale_color() +
geom_line(aes(y=Lower, colour=Right)) +
geom_line(aes(y=Upper, colour=Right)) +
scale_linetype_discrete(name="Group") +
scale_color_discrete(name="Limits")
Created on 2021-06-25 by the reprex package (v1.0.0)

Resources