I'm trying to create a graph like this:
I'm using ggplot2
function geom_linerange
and geom_point
to do this. In my graph I would like to put the year variable on the y axis, the coefficient on the x axis and the confidence interval with the geom_linerange
but I am getting this error:
Error: Invalid input: date_trans works with objects of class Date only
If I remove the geom_linerange
, the code works.
My code:
library(tidyverse)
results %>%
group_by(Year) %>%
ggplot(aes(x = Coefficient, y = Year, color = Variable))
geom_point()
geom_linerange(aes(x = Variable, ymin = conf.low_95, ymax = conf.high_95))
My dput
:
structure(list(Variable = c("Model 1", "Model 2", "Model 3",
"Model 4", "Model 5", "Model 6", "Model 1", "Model 2", "Model 3",
"Model 4", "Model 5", "Model 6", "Model 1", "Model 2", "Model 3",
"Model 4", "Model 5", "Model 6", "Model 1", "Model 2", "Model 3",
"Model 4", "Model 5", "Model 6", "Model 1", "Model 2", "Model 3",
"Model 4", "Model 5", "Model 6", "Model 1", "Model 2", "Model 3",
"Model 4", "Model 5", "Model 6"), Coefficient = c(-0.337563422687641,
-0.339870358919705, -0.174601667810286, -0.822153207427548, 0.0984725028254042,
-0.302360726656967, -0.0144099316467553, 0.394261621304049, 0.408314375266859,
-0.0437332910230634, 0.197918449352892, 0.211892879792213, -0.333777406931884,
-0.211101666108092, -0.191240973701465, -0.200312029595054, 0.271332017169336,
0.0198772428073128, -0.271118540022818, -0.296438420884506, -0.1959350798131,
-0.604938094557287, -0.347759168324679, -0.565499762731695, 0.04352845127211,
0.00464668179125901, 0.0618870586543521, 0.0154152406576902,
-0.0882820982782311, -0.300512132812594, 0.354012886011799, 0.204557246817836,
0.239302395932748, 0.419316974288796, -0.196096726024389, -0.0990227142573055
), conf.low_95 = c(-0.659882600540782, -0.652153038033736, -0.592355494245245,
-1.15887028037482, -0.0957338272150386, -0.653747543529451, -0.360778831131537,
-0.0547889861473285, -0.0813328193407771, -0.362272084151103,
-0.00614433448580284, -0.0851580894901174, -0.690820142512398,
-0.591538313876884, -0.665799398871222, -0.524239633810207, -0.00729694825470084,
-0.312432149478429, -0.731712380556692, -0.742564839785077, -0.644569229627752,
-1.00362824114186, -0.646168503067745, -0.872199479720429, -0.295291773790078,
-0.366647571941097, -0.278411009308147, -0.308350900982826, -0.328643095941367,
-0.598339525727505, -0.161377035034601, -0.674101088034365, -0.360447691535622,
-0.0792532689562487, -0.541552105303034, -0.668221935319178),
conf.high_95 = c(-0.0152442448344994, -0.0275876798056746,
0.243152158624673, -0.485436134480278, 0.292678832865847,
0.0490260902155157, 0.331958967838026, 0.843312228755427,
0.897961569874495, 0.274805502104976, 0.401981233191587,
0.508943849074543, 0.0232653286486298, 0.1693349816607, 0.283317451468292,
0.1236155746201, 0.549960982593372, 0.352186635093054, 0.189475300511056,
0.149687998016065, 0.252699070001552, -0.206247947972715,
-0.049349833581613, -0.258800045742961, 0.382348676334298,
0.375940935523615, 0.402185126616851, 0.339181382298207,
0.152078899384905, -0.00268473989768353, 0.869402807058199,
1.08321558167004, 0.839052483401118, 0.917887217533841, 0.149358653254257,
0.470176506804567), Year = structure(c(17897, 17897, 17897,
17897, 17897, 17897, 18262, 18262, 18262, 18262, 18262, 18262,
18628, 18628, 18628, 18628, 18628, 18628, 18993, 18993, 18993,
18993, 18993, 18993, 19358, 19358, 19358, 19358, 19358, 19358,
19723, 19723, 19723, 19723, 19723, 19723), class = "Date")), row.names = c(NA,
-36L), class = c("tbl_df", "tbl", "data.frame"))
CodePudding user response:
I think there are two issues:
I'm inferring that
Coefficient
and theconf.*
variables should be on the same axis, but you are plottingx=Coefficient
and theny*=conf*
in the linerange geom. I suspect they should both be on the y-axis.Your main mapping has (after swapping for the above)
x=Year
, but your linerange geom mapsx=Variable
, a categorical variable. Since you includecolor=Variable
in the first mapping, I'll infer that that should be inherited in your linerange, so I'll remove thex=
mapping and let it inherit.
In the end, this is a reasonable start:
results %>%
group_by(Year) %>%
ggplot(aes(y = Coefficient, x = Year, color = Variable))
geom_point()
geom_linerange(aes(ymin = conf.low_95, ymax = conf.high_95))
Side note ... it might be useful to break those lineranges apart a little. Here's a suggestion, though it is a bit fragile in that it relies on "120 days" as the dodging width, and this will be sensitive to the range of data years.
results %>%
group_by(Year) %>%
ggplot(aes(y = Coefficient, x = Year, color = Variable))
geom_point(position=position_dodge(width=120))
geom_linerange(aes(ymin = conf.low_95, ymax = conf.high_95), position=position_dodge(width=120))
This obviously won't work if the date samples are more closely-spaced. Just a thought :-)