full_join adding extra rows and NA in r - r

I have two data frames that I am trying to join using full_join, here is a subset of my data:
df1 <- structure(list(Team = structure(c(4L, 3L, 5L, 6L, 7L, 7L, 8L,
8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 14L, 13L, 15L, 15L,
16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 22L, 23L,
23L, 24L, 24L, 25L, 25L, 28L, 28L, 29L, 29L, 30L, 30L, 31L, 31L,
32L, 32L, 33L, 33L, 34L, 34L, 2L, 1L, 26L, 27L), .Label = c("76ers",
"76ers ", "Bucks", "Bucks ", "Bull ", "Bulls ", "Cavaliers ",
"Celtics ", "Clippers ", "Grizzlies ", "Hawks ", "Heat ", "Hornets",
"Hornets ", "Jazz ", "Kings ", "Knicks ", "Lakers ", "Magic ",
"Mavericks ", "Net ", "Nets ", "Nuggets ", "Pacers ", "Pelicans ",
"Pistons", "Pistons ", "Raptors ", "Rockets ", "Spurs ", "Thunder ",
"Timberwolves ", "Warriors ", "Wizards "), class = "factor"),
Injury.Count = c(3L, 3L, 1L, 1L, 1L, 2L, 0L, 2L, 1L, 1L,
0L, 2L, 1L, 0L, 5L, 4L, 3L, 2L, 3L, 0L, 3L, 3L, 4L, 6L, 5L,
0L, 2L, 2L, 1L, 2L, 0L, 1L, 3L, 4L, 2L, 6L, 2L, 1L, 1L, 1L,
3L, 3L, 4L, 5L, 1L, 6L, 4L, 2L, 0L, 2L, 2L, 1L, 5L, 6L, 1L,
1L), HomevsAway = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L), .Label = c("0", "1"), class = "factor")), row.names = c(NA,
-56L), class = "data.frame")
df2 <- structure(list(Team = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 4L,
5L, 6L, 7L, 8L, 9L, 9L, 10L, 10L, 11L, 12L, 12L, 13L, 13L, 14L,
15L, 15L, 16L, 16L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L,
22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L, 26L, 26L, 27L, 28L, 28L,
3L, 5L, 6L, 7L, 8L, 11L, 14L, 17L, 27L), .Label = c("76ers",
"Bucks", "Bulls", "Cavaliers", "Celtics", "Clippers", "Grizzlies",
"Hawks", "Heat", "Hornets", "Jazz", "Kings", "Knicks", "Lakers",
"Magic", "Mavericks", "Nets", "Nuggets", "Pacers", "Pelicans",
"Pistons", "Raptors", "Rockets", "Spurs", "Thunder", "Timberwolves",
"Warriors", "Wizards"), class = "factor"), HomevsAway = structure(c(1L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
t_1 = c(55.883, 140.1, 32.2, 37.967, 29.85, 24.317, 57.316,
17.967, 19.05, 36.95, 16.167, 95.317, 86.533, 21.334, 52.567,
40.75, 28.3, 68.15, 97.067, 102.233, 26.866, 71.033, 34.467,
24.233, 42.033, 22.433, 59.033, 41.516, 12.7, 107.996, 6.5,
32.783, 0, 23.217, 13.93, 0, 54.88, 23.617, 83.834, 106.794,
17.56, 27.76, 85.83, 0.017, 35.183, 22.467, 25.033, 0, 0,
0, 0, 0, 0, 0, 0, 0), t_3 = c(197.3164, 388.6827, 126.2663,
111.916, 61.95, 91.55, 167.067, 104.083, 71.067, 135.383,
45.633, 261.317, 267.399, 114.6997, 159.2, 152.034, 84.8337,
204.3003, 351.449, 376.317, 86.333, 213.9, 99.767, 65.1,
131.767, 73.317, 126.416, 129.066, 73.383, 347.0994, 4761,
113.367, 0, 89.933, 59.8, 0, 188.983, 124.384, 215.666, 289.9667,
92, 144.2497, 254.083, 32.0333, 122.1837, 102.533, 82.817,
0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -56L), groups = structure(list(
Team = structure(1:28, .Label = c("76ers", "Bucks", "Bulls",
"Cavaliers", "Celtics", "Clippers", "Grizzlies", "Hawks",
"Heat", "Hornets", "Jazz", "Kings", "Knicks", "Lakers", "Magic",
"Mavericks", "Nets", "Nuggets", "Pacers", "Pelicans", "Pistons",
"Raptors", "Rockets", "Spurs", "Thunder", "Timberwolves",
"Warriors", "Wizards"), class = "factor"), .rows = structure(list(
1:2, 3:4, c(5L, 48L), 6:7, c(8L, 49L), c(9L, 50L), c(10L,
51L), c(11L, 52L), 12:13, 14:15, c(16L, 53L), 17:18,
19:20, c(21L, 54L), 22:23, 24:25, c(26L, 55L), 27:28,
29:30, 31:32, 33:34, 35:36, 37:38, 39:40, 41:42, 43:44,
c(45L, 56L), 46:47), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 28L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
I have tried to join the data using full_join:
LR <- full_join(df1, df2, by = c("HomevsAway", "Team"))
The output of LR I am receiving is giving me extra rows with random NA's involved. My expected output should be a 56rowx5col table.

The problem is that in your first data.frame the Team names have a trailing space. This means that instead of the string "Bucks" you have the string "Bucks ".
These two strings cannot be joined.
Here is how you can fix your data. First delete leading spaces using sub and then transform the Team into a character vector. Then the full_join works as planned:
df1_new <- df1 %>% as_tibble() %>%
mutate(Team = sub(" +", "", as.character(Team)))
df2_new <- df2 %>%
mutate(Team = as.character(Team))
df1_new %>% full_join(df2_new, by = c("Team", "HomevsAway"))
# A tibble: 58 x 5
Team Injury.Count HomevsAway t_1 t_3
<chr> <int> <fct> <dbl> <dbl>
1 Bucks 3 0 32.2 126.
2 Bucks 3 1 38.0 112.
3 Bull 1 0 NA NA
4 Bulls 1 1 0 0
5 Cavaliers 1 0 24.3 91.6
6 Cavaliers 2 1 57.3 167.
7 Celtics 0 0 0 0
8 Celtics 2 1 18.0 104.
9 Clippers 1 0 0 0
10 Clippers 1 1 19.0 71.1
Note that there are still a few NAs. This is due to some typos: Bull vs Bulls and Net vs Nets.

Related

R:Error in `[.data.frame` undefined columns selected

I have this data sample
dput()
timeseries=structure(list(sales_point_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), calendar_id_operday = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L), line_fact_amt = c(55767L,
59913L, 36363L, 48558L, 505L, 76344L, 22533L, 11965L, 78944L,
36754L, 30621L, 55716L, 32470L, 62165L, 57986L, 2652L, 16487L,
72849L, 73715L, 65656L, 64411L, 47460L, 61866L, 10877L, 72392L,
53011L, 23544L, 76692L, 10388L, 24255L, 56684L, 59329L, 6655L,
65612L, 17495L, 10389L, 63702L, 47407L, 78782L, 22898L, 21151L,
32587L)), class = "data.frame", row.names = c(NA, -42L))
calendar_id_operday=1 its mean range of week 20210101-20210108(ymd) but here there is no date format only week, just such a specificity of these data . I try transform my data.
library(reshape)
df <- cast(melt(timeseries, id=c("calendar_id_operday"), na.rm=TRUE),
line_fact_amt + calendar_id_operday)[, c("line_fact_amt", "calendar_id_operday", substring(month.name, 1, 3))]
colnames(df)[1] <- "sales_point_id"
df[, substring(month.name, 1, 3)] <- lapply(timeseries[, substring(month.name, 1, 3)],
function(x) as.numeric(as.character(x)))
But something goes wrong
Error in `[.data.frame`(timeseries, , substring(month.name, 1, 3)) :
undefined columns selected
I want that as the result i got this data.frame
sales_point_id year jan-1 jan-2 jan-3 jan-4 feb1
1 1 2021 8034.843 7485.725 8238.493 8446.994 134
2 1 2021 7810.315 7261.198 8013.965 8222.466 346
3 1 2021 7585.788 7036.670 7789.438 7997.938 54364
4 1 2021 7361.260 6812.142 7564.910 7773.411 34546
5 1 2021 7136.733 6587.615 7340.382 7548.883 46436
jan-1 is data for firts week of jan. jan2- is the second week of jan and so on.
What should i do to get desired result?
Thanks for your valuable help

Margins package in R generating NAs in results

This is my first post, so hopefully I'm doing this right.
I'm running a polr model in R and am attempting to generate some marginal effects plots using the margins package. When I view the results, everything except for the AME has a value of NA. Can anyone help me figure out what I'm doing wrong?
Here's a copy of my results:
factor AME SE z p lower upper
Gender -0.0452 NA NA NA NA NA
IsNonwhite 0.0087 NA NA NA NA NA
PartyNumeric 0.1898 NA NA NA NA NA
PartyStrength -0.0358 NA NA NA NA NA
And here's a copy of my code:
debate <- debate %>%
mutate(UnfairIndex = as.numeric((debate$UnfairCountTrump-debate$UnfairCountBiden)))
debate<- debate%>%
mutate(AbsUnfairIndex = abs(debate$UnfairIndex))
debate <- debate %>%
mutate(X7PartyScale=case_when(debate$StrongRep ==1 ~ 3,
debate$StrongRep ==2 ~ 2,
debate$StrongDem ==1 ~ -3,
debate$StrongDem ==2 ~ -2,
debate$IndCloser ==1 ~ 1,
debate$IndCloser ==2 ~ -1,
debate$IndCloser ==3 ~ 0))
debate<- debate %>%
mutate(PartyFactor=as.factor(case_when(debate$X7PartyScale==-3 ~ "Democrat",
debate$X7PartyScale==-2 ~ "Democrat",
debate$X7PartyScale==-1 ~ "Democrat",
debate$X7PartyScale==0 ~ "Independent",
debate$X7PartyScale==1 ~ "Republican",
debate$X7PartyScale==2 ~ "Republican",
debate$X7PartyScale==3 ~ "Republican")))
debate<- debate %>%
mutate(PartyStrength= abs(debate$X7PartyScale))
debate<- debate %>%
mutate(PartyNumeric = as.numeric(PartyFactor) - 1)
interact<- polr(factor(AbsUnfairIndex) ~ PartyStrength + PartyNumeric + PartyStrength*PartyNumeric + Gender + IsNonwhite, data=debate, Hess=TRUE)
summary(interact)
interactmargins2<- margins(interact)
summary(interactmargins2)
Editing to provide a sample of the data:
structure(list(Consent = c(1L, 1L, 1L, 1L, 1L, 1L), Gender = c(2L,
2L, 2L, 2L, 1L, 1L), IsFemale = c(0L, 0L, NA, 0L, NA, 1L), Age = c(19L,
19L, 19L, 19L, 19L, 20L), Race = c(1L, 1L, 4L, 1L, 4L, 1L), IsNonwhite = c(0L,
0L, 1L, 0L, 1L, 0L), PartyID = c(1L, 2L, 1L, 1L, 1L, 1L), StrongDem = c(1L,
NA, 1L, 1L, 2L, 2L), StrongRep = c(NA, 1L, NA, NA, NA, NA), IndCloser = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
), DemImportant = c(1L, NA, 2L, 1L, 2L, 3L), DemDescribe = c(2L,
NA, 2L, 1L, 2L, 2L), DemWeThey = c(2L, NA, 2L, 2L, 3L, 4L), DemThink = c(1L,
NA, 2L, 1L, 2L, 1L), RepImportant = c(NA, 1L, NA, NA, NA, NA),
RepDescribe = c(NA, 1L, NA, NA, NA, NA), RepWeThey = c(NA,
2L, NA, NA, NA, NA), RepThink = c(NA, 1L, NA, NA, NA, NA),
FeelingThermTrump = c(0L, 100L, 0L, 0L, 0L, NA), FeelingThermBiden = c(64L,
NA, 100L, 95L, 50L, 100L), WatchLive = c(1L, 1L, 1L, 1L,
1L, 1L), WatchAfter = c(1L, 1L, 1L, 1L, 1L, 1L), WatchDebate = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("", "WatchAll", "WatchNone",
"WatchSome"), class = "factor"), FollowCoverage = c(1L, 1L,
1L, 1L, 2L, 1L), FairnessHonesty = c(4L, 2L, 5L, 4L, 5L,
4L), WhoUnfair = structure(c(5L, 4L, 6L, 5L, 9L, 5L), .Label = c("",
"1,2", "1,2,3", "1,3", "2", "2,3", "3", "3,4", "4"), class = "factor"),
WhoDishonest = structure(c(6L, 2L, 6L, 6L, 5L, 6L), .Label = c("",
"Biden", "Moderator", "More than one", "Not Sure", "Trump"
), class = "factor"), TrumpFairnessRater = c(0L, 9L, 1L,
2L, 5L, NA), BidenFairnessRater = c(7L, 2L, 9L, 7L, 5L, 10L
), WallaceFairnessRater = c(10L, 2L, 6L, 4L, 8L, 10L), TrumpHonestyRater = c(1L,
10L, 1L, 2L, 5L, NA), BidenHonestyRater = c(6L, 2L, 9L, 10L,
5L, 10L), WallaceHonestyRater = c(10L, 0L, 6L, 8L, 8L, 10L
), Voting1 = c(1L, 1L, 1L, 1L, 1L, 1L), WhoVote1 = c(2L,
1L, 2L, 2L, 2L, 2L), WhoVoteLean1 = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), TrustGov = c(3L,
3L, 4L, 3L, 2L, 5L), Q4_First.Click = c(14.215, 460.794,
160.691, 0, 8.308, 0), Q4_Last.Click = c(356.559, 461.175,
407.781, 0, 104.224, 0), Q4_Page.Submit = c(390.864, 461.601,
414.195, 449.768, 415.194, 414.337), Q4_Click.Count = c(24L,
2L, 8L, 0L, 3L, 0L), BidenInterruptTrump = c(21L, 19L, 21L,
22L, 20L, 25L), TrumpInterruptBiden = c(19L, 21L, 19L, 19L,
19L, 19L), ModeratorInterruptTrump = c(24L, 20L, 22L, 20L,
19L, 25L), ModeratorInterruptBiden = c(24L, 24L, 20L, 21L,
21L, 21L), BidenAggressive = c(20L, 18L, 22L, 20L, 20L, 24L
), TrumpAggressive = c(18L, 19L, 18L, 18L, 18L, 18L), ModeratorAggressivetoTrump = c(20L,
19L, 20L, 19L, 19L, 19L), ModeratorAggressivetoBiden = c(22L,
23L, 20L, 21L, 20L, 21L), BidenLie = c(8L, 6L, 8L, 9L, 9L,
8L), TrumpLie = c(8L, 11L, 8L, 10L, 10L, 8L), BidenMischarTrump = c(12L,
12L, 14L, 10L, 12L, 15L), TrumpMischarBiden = c(9L, 12L,
9L, 10L, 11L, 9L), ModeratorFavorTrump = c(13L, 14L, 10L,
13L, 11L, 12L), ModeratorFavorBiden = c(11L, 8L, 11L, 9L,
11L, 10L), UnfairCountTrump = c(3L, 4L, 4L, 2L, 1L, 1L),
UnfairCountBiden = c(5L, 2L, 5L, 3L, 2L, 5L), WhoVote2 = c(2L,
1L, 2L, 2L, 2L, 2L), WhoVoteLean2 = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_), TrustGov.1 = c(3L,
3L, 4L, 3L, 3L, 3L), OfficialsCare = c(3L, 3L, 3L, 3L, 5L,
7L), LoseTouch = c(2L, 4L, 1L, 3L, 3L, 2L), OnlyCareVotes = c(2L,
4L, 1L, 4L, 2L, 1L)), row.names = c(NA, 6L), class = "data.frame")

Adding Text to a Facet Grid

I am till having trouble with this, so I have edited my question (below) to include my data.
I would like to add two different labels (the respective sample sizes) to each of the sections of the grid.
I know I need to use geom_text, but I just can't seem to get it to work. Everything I have tried has returned errors. Here is the code I used to make my graph so far:
ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+
geom_point()+
geom_line()+
facet_grid(Type ~.)+
geom_smooth(method = 'lm', formula = y ~ poly(x), colour= "black", aes(group=1), se= FALSE)+
theme(legend.position="none") +
labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years")
Someone mentioned that it might be helpful if I included the following:
> dput(Q)
structure(list(StudentFactor = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L,
9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L,
23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 26L,
26L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 28L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 31L, 31L, 31L, 32L, 32L, 32L, 32L, 32L, 32L, 33L, 33L, 33L,
33L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L,
35L, 35L, 35L, 35L, 35L, 35L, 36L, 36L, 36L), .Label = c("789331",
"796882", "805933", "826523", "827911", "830271", "831487", "832929",
"834598", "836364", "838607", "839802", "841903", "843618", "852125",
"855524", "873527", "876406", "879972", "885409", "885650", "888712",
"894218", "903303", "928026", "952797", "955389", "956952", "957206",
"957759", "959200", "962490", "965873", "967416", "968728", "969005"
), class = "factor"), Question = structure(c(1L, 1L, 1L, 1L,
1L, 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 2, 0, -1, 1, 0, 0, 1, 0, 0, 0,
0, 0, -1, 1, 0, 0, 0, -1, 0, 1, -1, 0, 0, 0, 1, -1, 1, -1,
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1, 1, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, -1, 1, 0, 0, 1, 0, 0, 0, 0, -2, 2, 0, 0, 0,
0, 0, 1, 0, 1, -1, 0, 1, -1, 0, 1, 2, -1, 0, 1, 0, 1, -1,
1, 0, -1, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, -1, 1, -1,
1, -1, -3, 3, 0, 0, -1, 0, -1, 0, 1, 1, 0, 0, 1, 0, 0, 1,
-1, 1, -1, 0, -2, 2, -1, -1, 1, 0, 0)), class = "data.frame", row.names = c(NA,
-231L), .Names = c("StudentFactor", "Question", "Type", "week",
"Rating"))
Earlier Question:
I have used the following code to make this graph:
ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+
geom_point()+
geom_line()+
facet_grid(Q$Type ~.)+
geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))+
theme(legend.position="none") +
labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years")
However, as soon as I try to add an annotation to include the sample size for each cohort, I get the following error:
Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(2L, 2L, 2L, 2L, :
replacement has 231 rows, data has 1
This is the code I am trying to use:
ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+
geom_point()+
geom_line()+
facet_grid(Q$Type ~.)+
geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))+
theme(legend.position="none") +
labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years")+
annotate("text", x = 1, y=4, label = "N=")
I am a complete beginner at R. Any help would be appreciated! Thank you in advance!
I guess, you would like to label each panel with the respective sample size, or to add the total sample size just in one place. In that case annotate will be of no use (see for example this question), but you can use geom_text instead:
You would create a separate data.frame for your labels, which you then use inside of geom_text:
library(ggplot2)
labels <- tibble::tribble(~mpg, ~hp, ~cyl, ~label,
15, 200, 4, "label 1",
15, 150, 6, "label 2")
ggplot(mtcars, aes(mpg, hp)) +
geom_point() +
facet_wrap(~cyl) +
geom_text(data = labels, aes(label = label))
(Since you didn't provide data which would make it possible to reproduce your graphs, I simply used mtcars to illustrate the problem.)
Edit:
Using your data I adapted the code. First you create a data.frame for your label(s):
labels <- data.frame(
x = 2.5,
y = 3,
Type = "FYS",
label = "mylabel"
)
Then you simply add the following line to your code:
... +
geom_text(data = labels, aes(x = x, y = y, label = label), inherit.aes = FALSE)
This approach is slightly different than what I showed above: Either you supply the coordinates according to your variables, which you use in the rest of the plot, or you give them new names, use inherit.aes = FALSE and set the new variables with aes as demonstrated with your data.
Full code:
labels <- data.frame(
x = 2.5,
y = 3,
Type = "FYS",
label = "mylabel"
)
ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+
geom_point()+
geom_line()+
facet_grid(Type ~ .)+
geom_smooth(method = 'lm', formula = y ~ poly(x), colour= "black", aes(group=1), se= FALSE)+
theme(legend.position="none") +
labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years") +
geom_text(data = labels, aes(x = x, y = y, label = label), inherit.aes = FALSE)
Your error is due to using facet_grid incorrectly. Replace your line with facet_grid(Type ~.)+ instead of Q$Type. For example:
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
facet_grid(iris$Species ~ .) +
annotate("text", x = 7, y = 4, label = "foo")
Error in '$<-.data.frame'('*tmp*', "PANEL", value = c(1L, 1L, 1L, 1L, :
replacement has 150 rows, data has 1
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
facet_grid(Species ~ .) +
annotate("text", x = 7, y = 4, label = "foo")
No error:

plot group and category means with group_by

I am new to R and trying to figure out a way to plot means for individual samples as well as group means with ggplot.
I am following this articles on R-bloggers (last paragraph):
https://www.r-bloggers.com/plotting-individual-observations-and-group-means-with-ggplot2/
This is my code:
gd <- meanplot1 %>%
group_by(treatment, value) %>%
summarise(measurement = mean(measurement))
ggplot(meanplot1, aes(x=value, y=measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
geom_line(data=gd, size=3, alpha=0.9) +
theme_bw()
Whilst the sample means are being shown, the group means arenĀ“t. I get the error
geom_path: Each group consists of only one observation. Do you need
to adjust the group aesthetic?
Upon adding group=1, I get a weirdly mixed category mean, but not what I am looking for..
I scrolled through a lot of articles already, but couldnt find an answer - I would be so happy if somebody could help me out here!! :)
My data (meanplot1) is formatted like this:
treatment sample value measurement
1 control, control 1, initial, 20,
2 control, control 1, 26, NA,
3 control, control 1, 26', 28,
12 control, control 2, initial, 22,
13 control control 2, 26, NA,
14 control control 2, 26', 36,
15 control control 2, 28, 45,
67 stressed, stress 1, initial, 37,
68 stressed, stress 1, 26, NA,
69 stressed, stress 1, 26', 17,
78 stressed, stress 2, initial, 36,
79 stressed, stress 2, 26, NA,
80 stressed, stress 2, 26', 25,
I am hoping to see 6 lines, one mean for stress 1, stress 2, control 1 and control 2, and one mean for all treatment=control, and one for all treatment=stressed
output dput(gd):
structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("control", "stressed"), class = "factor"), value = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L), .Label = c("26", "26'", "28", "28'",
"30", "30'", "32", "32'", "34", "34'", "initial"), class = "factor"),
measurement = c(NA, 32.3333333333333, 39.5, 30.3333333333333,
31.8333333333333, 31.8333333333333, NA, 36, 34.6666666666667,
36, 24.6666666666667, NA, 25.3333333333333, 33.3333333333333,
32, 50.1666666666667, 39.1666666666667, NA, 33.5, 24.3333333333333,
27.3333333333333, 36)), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -22L), vars = list(treatment), drop = TRUE, .Names = c("treatment",
"value", "measurement"))
output dput(meanplot1):
structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control",
"stressed"), class = "factor"), sample = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("control 1",
"control 2", "control 3", "control 4", "control 5", "control 6",
"stress 1", "stress 2", "stress 3", "stress 4", "stress 5", "stress 6"
), class = "factor"), value = structure(c(11L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("26", "26'",
"28", "28'", "30", "30'", "32", "32'", "34", "34'", "initial"
), class = "factor"), measurement = c(20L, NA, 28L, 18L, 17L,
19L, 34L, NA, 23L, 29L, 27L, 22L, NA, 36L, 45L, 31L, 40L, 44L,
NA, 49L, 40L, 39L, 32L, NA, 35L, 57L, 30L, 37L, 29L, NA, 44L,
37L, 46L, 20L, NA, 39L, 27L, 30L, 40L, 25L, NA, 29L, 50L, 30L,
26L, NA, 28L, 45L, 47L, 27L, 35L, NA, 24L, 22L, 35L, 28L, NA,
28L, 45L, 27L, 28L, 24L, NA, 47L, 30L, 39L, 37L, NA, 17L, 29L,
29L, 31L, 29L, NA, 37L, 21L, 27L, 36L, NA, 25L, 41L, 51L, 66L,
50L, NA, 33L, 25L, 22L, 36L, NA, 33L, 45L, 26L, 72L, 59L, NA,
33L, 26L, 25L, 33L, NA, 21L, 33L, 25L, 29L, 21L, NA, 26L, 20L,
16L, 22L, NA, 30L, 27L, 28L, 57L, 41L, NA, 28L, 23L, 17L, 52L,
NA, 26L, 25L, 33L, 46L, 35L, NA, 44L, 31L, 57L)), .Names = c("treatment",
"sample", "value", "measurement"), class = "data.frame", row.names = c(NA,
-132L))
I suppose you are aiming to plot the treatment means.
By default, since you are using a categorical x-axis, the grouping is set to the interaction between x and color. You only want to group by treatment, however. So we'll add the correct grouping to the call.
ggplot(meanplot1, aes(x = value, y = measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
geom_line(aes(group = treatment), gd, size=3, alpha=0.9) +
theme_bw()
Also note that
ggplot(meanplot1, aes(x=value, y=measurement, color=treatment)) +
geom_line(aes(group=sample), alpha=0.3) +
stat_summary(aes(group = treatment), fun.y = mean, geom = 'line', size=3, alpha=0.9) +
theme_bw()
Gives the same plot, without the interruption.

Error in migrating a ggplot graph to ver. 0.9.3

Trying to migrate a ggplot graph to ver 0.9.3, the commented out line in the code below produces a Discrete value supplied to continuous scale error (which was not a problem before).
Can you help me correct it?
If you need the data to experiment with, I provide it below.
ggplot(mdfr, aes(as.Date(value, "%d/%m/%Y"), name, colour = factor(stadio))) +
geom_line(aes(size=rating)) +
labs(colour="Baseline/Actual :", x = "", y = "") +
scale_colour_brewer(palette="BrBG",breaks = c("1", "3", "6","8"),
labels = c("Label 1", "Label 2", "Label 3","Label 4")) +
scale_size_manual(breaks = levels(mdfr$rating), values = as.integer(levels(mdfr$rating)), guide = "none") +
theme_bw() +
#geom_vline(data=dfrDataDate, aes(xintercept= as.Date(data.date, "%Y-%m-%d")),colour=rgb(215, 25, 28, max = 255),size=1) +
geom_text(data=dfrDataDate, aes(x= as.Date(data.date, "%Y-%m-%d"), label = format(as.Date(data.date),"%d/%m/%Y")), hjust = -0.05, vjust = 1.5, colour = "darkred", size = 3 ) +
geom_text(data=dfrLabels, aes(x= as.Date(diag_date, "%d/%m/%Y"), label = format(as.Date(diag_date, "%d/%m/%Y"),"%d/%m/%Y")), hjust = 0.5, vjust = -1, colour = "black", size = 3 ) +
geom_text(data=dfrYpogr, aes(x= as.Date(ypogr_date, "%d/%m/%Y"), label = format(as.Date(ypogr_date, "%d/%m/%Y"),"%d/%m/%Y")), hjust = 0.5, vjust = -1, colour = "black", size = 3 ) +
scale_y_discrete(breaks=names, labels=new.names) +
ggtitle('New plot title') +
theme(plot.title = element_text(size=16),
legend.position = "top",
legend.title = element_text(size=12),
legend.text = element_text(size=10),
legend.key = element_rect(colour='white'),
axis.text.x = element_text(angle=0,size = 10),
axis.title.x = element_text(size=14),
axis.text.y = element_text(size=10),
axis.title.y = element_text(angle=90,size=14),
axis.ticks.length = unit(.05, "cm")
)
DATA needed to produce the graph:
mdfr <- structure(list(name = structure(c(22L, 22L, 22L, 22L, 20L, 20L,
20L, 20L, 18L, 18L, 18L, 18L, 16L, 16L, 16L, 16L, 14L, 14L, 14L,
14L, 12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L, 8L, 8L, 8L, 8L,
6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 21L, 21L, 21L,
21L, 19L, 19L, 19L, 19L, 17L, 17L, 17L, 17L, 15L, 15L, 15L, 15L,
13L, 13L, 13L, 13L, 11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 7L, 7L,
7L, 7L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 22L,
22L, 22L, 22L, 20L, 20L, 20L, 20L, 18L, 18L, 18L, 18L, 16L, 16L,
16L, 16L, 14L, 14L, 14L, 14L, 12L, 12L, 12L, 12L, 10L, 10L, 10L,
10L, 8L, 8L, 8L, 8L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 2L, 21L, 21L, 21L, 21L, 19L, 19L, 19L, 19L, 17L, 17L, 17L,
17L, 15L, 15L, 15L, 15L, 13L, 13L, 13L, 13L, 11L, 11L, 11L, 11L,
9L, 9L, 9L, 9L, 7L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 1L), .Label = c("733 A", "733 B", "725 A", "725 B",
"727 A", "727 B", "558 A", "558 B", "705 A", "705 B", "635 A",
"635 B", "737 A", "737 B", "719 A", "719 B", "700 A", "700 B",
"579 A", "579 B", "541 A", "541 B"), class = "factor"), stadio = c(2,
4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4,
5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5,
7, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8,
1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1,
3, 6, 8, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4,
5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5, 7, 2, 4, 5,
7, 2, 4, 5, 7, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8,
1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1, 3, 6, 8, 1,
3, 6, 8, 1, 3, 6, 8), variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("start_date",
"end_date"), class = "factor"), value = structure(c(3L, 18L,
20L, 36L, 2L, 14L, 24L, 38L, 7L, 7L, 7L, 31L, 9L, 15L, 27L, 34L,
4L, 19L, 21L, 37L, 1L, 9L, 23L, 33L, 8L, 13L, 25L, 32L, 10L,
16L, 28L, 29L, 5L, 12L, 26L, 35L, 6L, 17L, 22L, 39L, 11L, 17L,
22L, 30L, 3L, 18L, 20L, 36L, 2L, 14L, 24L, 31L, 50L, 50L, 50L,
56L, 15L, 52L, 55L, 32L, 48L, 49L, 55L, 34L, 1L, 53L, 53L, 57L,
49L, 51L, 54L, 58L, 10L, 16L, 28L, 29L, 5L, 12L, 26L, 35L, 6L,
17L, 22L, 39L, 11L, 17L, 22L, 30L, 18L, 20L, 36L, 45L, 14L, 24L,
38L, 46L, 7L, 7L, 31L, 42L, 15L, 27L, 34L, 44L, 19L, 21L, 37L,
44L, 9L, 23L, 33L, 40L, 13L, 25L, 32L, 44L, 16L, 28L, 29L, 41L,
12L, 26L, 35L, 43L, 17L, 22L, 39L, 47L, 17L, 22L, 30L, 47L, 18L,
20L, 36L, 59L, 14L, 24L, 31L, 64L, 50L, 50L, 56L, 66L, 52L, 55L,
32L, 61L, 49L, 55L, 34L, 63L, 53L, 53L, 57L, 65L, 51L, 54L, 58L,
60L, 16L, 28L, 29L, 63L, 12L, 26L, 35L, 62L, 17L, 22L, 39L, 67L,
17L, 22L, 30L, 61L), .Label = c("03/05/2012", "07/06/2011", "22/02/2011",
"22/06/2012", "23/12/2011", "28/12/2011", "29/02/2012", "29/03/2012",
"29/06/2012", "30/05/2011", "30/12/2011", "03/02/2012", "04/07/2012",
"08/02/2012", "10/07/2012", "17/10/2011", "23/02/2012", "24/05/2011",
"25/07/2012", "01/06/2011", "01/08/2012", "02/03/2012", "05/07/2012",
"09/02/2012", "12/07/2012", "16/02/2012", "19/07/2012", "27/01/2012",
"06/04/2012", "07/06/2012", "08/05/2012", "09/10/2012", "11/09/2012",
"16/10/2012", "18/05/2012", "20/09/2011", "23/10/2012", "24/04/2012",
"31/05/2012", "04/02/2013", "09/09/2012", "12/11/2012", "19/11/2012",
"28/02/2013", "28/09/2012", "29/10/2012", "30/11/2012", "01/07/2012",
"06/07/2012", "22/03/2012", "02/08/2012", "17/07/2012", "31/07/2012",
"06/09/2012", "26/07/2012", "12/06/2012", "13/11/2012", "20/11/2012",
"17/01/2013", "21/05/2013", "21/12/2012", "22/07/2012", "28/12/2012",
"30/03/2013", "30/04/2013", "31/01/2013", "31/12/2012"), class = "factor"),
rating = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("3", "5"), class = "factor")), row.names = c(NA,
-176L), .Names = c("name", "stadio", "variable", "value", "rating"
), class = "data.frame")
dfrDataDate <- structure(list(name = structure(1L, .Label = "733 A", class = "factor"),
data.date = structure(1L, .Label = "2013-01-02", class = "factor")), .Names = c("name",
"data.date"), row.names = c(NA, -1L), class = "data.frame")
dfrLabels <- structure(list(name = c("541 A", "579 A", "700 A", "719 A", "737 A",
"635 A", "705 A", "558 A", "727 A", "725 A", "733 A"), diag_date = c("20/09/2011",
"08/05/2012", "12/06/2012", "09/10/2012", "16/10/2012", "13/11/2012",
"20/11/2012", "06/04/2012", "18/05/2012", "31/05/2012", "07/06/2012"
)), .Names = c("name", "diag_date"), row.names = c(135L, 139L,
143L, 147L, 151L, 155L, 159L, 163L, 167L, 171L, 175L), class = "data.frame")
dfrYpogr <- structure(list(name = c("541 A", "579 A", "700 A", "719 A", "737 A",
"635 A", "705 A", "558 A", "727 A", "725 A", "733 A"), ypogr_date = c("17/01/2013",
"30/03/2013", "31/01/2013", "21/12/2012", "28/12/2012", "30/04/2013",
"21/05/2013", "28/12/2012", "22/07/2012", "31/12/2012", "21/12/2012"
)), .Names = c("name", "ypogr_date"), row.names = c(136L, 140L,
144L, 148L, 152L, 156L, 160L, 164L, 168L, 172L, 176L), class = "data.frame")
names <- as.character(unique(mdfr$name))
new.names <- c("No.541", "No.579", "No.700", "No.719", "No.737", "No.635",
"No.705", "No.558", "No.727", "No.725", "No.733", "", "",
"", "", "", "", "", "", "", "", "")
Adding of as.numeric() around as.Date() in call to geom_vline() makes red line to apear.
geom_vline(data=dfrDataDate, aes(xintercept= as.numeric(as.Date(data.date, "%Y-%m-%d"))),colour=rgb(215, 25, 28, max = 255),size=1)

Resources