#r #ggplot2
#r #ggplot2
Вопрос:
Я работаю над линейным графиком с двумя фасетами. Вот набор данных:
to_plot <- structure(list(date = structure(c(12600, 12600, 12600, 12600,
12631, 12631, 12631, 12631, 12662, 12662, 12662, 12662, 12692,
12692, 12692, 12692, 12723, 12723, 12723, 12723, 12753, 12753,
12753, 12753, 12784, 12784, 12784, 12784, 12815, 12815, 12815,
12815, 12843, 12843, 12843, 12843, 12874, 12874, 12874, 12874,
12904, 12904, 12904, 12904, 12935, 12935, 12935, 12935, 12965,
12965, 12965, 12965, 12996, 12996, 12996, 12996, 13027, 13027,
13027, 13027, 13057, 13057, 13057, 13057, 13088, 13088, 13088,
13088, 13118, 13118, 13118, 13118, 16344, 16344, 16344, 16344,
16375, 16375, 16375, 16375, 16405, 16405, 16405, 16405, 16436,
16436, 16436, 16436, 16467, 16467, 16467, 16467, 16495, 16495,
16495, 16495, 16526, 16526, 16526, 16526, 16556, 16556, 16556,
16556, 16587, 16587, 16587, 16587, 16617, 16617, 16617, 16617,
16648, 16648, 16648, 16648, 16679, 16679, 16679, 16679, 16709,
16709, 16709, 16709, 16740, 16740, 16740, 16740, 16770, 16770,
16770, 16770, 16801, 16801, 16801, 16801, 16832, 16832, 16832,
16832, 16861, 16861, 16861, 16861, 16892, 16892, 16892, 16892,
16922, 16922, 16922, 16922, 16953, 16953, 16953, 16953, 16983,
16983, 16983, 16983, 17014, 17014, 17014, 17014, 17045, 17045,
17045, 17045, 17075, 17075, 17075, 17075, 17106, 17106, 17106,
17106, 17136, 17136, 17136, 17136, 17167, 17167, 17167, 17167,
17198, 17198, 17198, 17198, 17226, 17226, 17226, 17226, 17257,
17257, 17257, 17257, 17287, 17287, 17287, 17287, 17318, 17318,
17318, 17318, 17348, 17348, 17348, 17348, 17379, 17379, 17379,
17379, 17410, 17410, 17410, 17410, 17440, 17440, 17440, 17440
), class = "Date"), wave = c("Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV", "Pegida", "Hartz_IV", "Pegida", "Hartz_IV", "Pegida",
"Hartz_IV"), type = c("Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity", "Issue salience", "Issue salience", "Party popularity",
"Party popularity"), value = c(NA, 71.3381555153707, NA, 9.91085474567383,
NA, 78.1039755351682, NA, 11.2076455256299, NA, 73.9750445632799,
NA, 10.0549882168107, NA, 72.5748502994012, NA, 9.25831202046036,
NA, 71.8585622443016, NA, 8.41949778434269, NA, 68.7237866986219,
NA, 7.89902280130293, NA, 71.9364161849711, NA, 7.74731823599523,
NA, 79.7647058823529, NA, 5.15384615384615, NA, 87.2630331753554,
NA, 6.97674418604651, NA, 80.3149606299213, NA, 7.27708916433427,
NA, 79.9878345498783, NA, 9.35085007727975, NA, 78.6594529606252,
NA, 13.2875143184422, NA, 75.5769792579609, NA, 16.243847027641,
NA, 79.1672711446395, NA, 13.2208418320044, NA, 77.2151898734177,
NA, 10.7784431137725, NA, 70.7051032333463, NA, 9.9642431466031,
NA, 71.5869311551925, NA, 12.0183136207554, NA, 74.00828892836,
NA, 9.932785660941, 20.4423022398639, NA, 8.86486486486486, NA,
29.1895010095183, NA, 5.49249359209081, NA, 31.1304347826087,
NA, 4.83056957462149, NA, 42.4705882352941, NA, 5.56177678238149,
NA, 32.7455919395466, NA, 6.75039246467818, NA, 28.3888228299643,
NA, 5.50493545937737, NA, 44.1956393635828, NA, 4.93920972644377,
NA, 36.2768496420048, NA, 3.50467289719626, NA, 30.7692307692308,
NA, 4.04485382458951, NA, 48.7492373398414, NA, 3.04975922953451,
NA, 72.1604938271605, NA, 3.69761709120789, NA, 81.8697729988053,
NA, 5.14764565043895, NA, 85.4637336504162, NA, 6.75729708116753,
NA, 80.3231390652048, NA, 9.53678474114441, NA, 78.9630512514899,
NA, 9.58795562599049, NA, 79.6600403341976, NA, 12.6798910929599,
NA, 80.632183908046, NA, 10.5672105672106, NA, 67.6506024096386,
NA, 12.298682284041, NA, 68.7082728592163, NA, 10.3050595238095,
NA, 61.9558735837806, NA, 12.5674633770239, NA, 61.1953352769679,
NA, 10.8452335005789, NA, 50.6347800413345, NA, 9.35446463084654,
NA, 58.288125376733, NA, 10.5347166799681, NA, 58.3232077764277,
NA, 11.3076923076923, NA, 56.4117822076763, NA, 10.9069585613761,
NA, 44.9571132800946, NA, 10.0578034682081, NA, 52.0665901262916,
NA, 9.13818722139673, NA, 48.3361534122955, NA, 9.9236641221374,
NA, 44.424882629108, NA, 7.58468335787923, NA, 43.4916718075262,
NA, 6.68764752163651, NA, 41.0905002810568, NA, 6.81818181818182,
NA, 31.9174041297935, NA, 6.67634252539913, NA, 34.3841214244016,
NA, 6.72238372093023, NA, 35.1091703056769, NA, 6.96480938416422,
NA, 37.210186240973, NA, 7.85042100049529, NA, 32.9296527159394,
NA, 7.52759381898455, NA, 33.3132892363199, NA, 8.8734835355286,
NA)), row.names = c(NA, -220L), class = c("tbl_df", "tbl", "data.frame"
))
это два периода времени, для которых я хотел бы иметь два фасета со «свободной» осью x.
Я пробовал версии следующего кода:
ggplot(to_plot, aes(x=date, y=value, linetype=type))
geom_line()
# facet_wrap(~ wave, scales="free")
facet_grid(. ~ wave, scales = "free_x")
scale_linetype_manual(breaks = c("Party popularity", "Issue salience"),
values = c("solid", "dashed"))
ylab("Share (percentage point)")
scale_x_date(date_labels = "%b-%Y", date_breaks = "5 month", date_minor_breaks = "1 month",
limits = c(min(to_plot$date), max = max(to_plot$date)), expand=c(0,0))
theme_bw()
theme(legend.title=element_blank(), legend.position="bottom",
legend.key.width = unit(2,"cm"), axis.title.x = element_blank())
К сожалению, я не смог заставить фасеты работать правильно. Я не уверен, что здесь происходит, я был бы признателен за любой намек!
Ответ №1:
Вы указываете ограничения на оси фасетов, которые вам нужно удалить. Вы также должны отфильтровать все строки, которые есть NA
в value
столбце:
ggplot(dplyr::filter(to_plot, !is.na(value)),
aes(x=date, y=value, linetype=type))
geom_line(na.rm = TRUE)
facet_grid(. ~ wave, scales = "free_x")
scale_linetype_manual(breaks = c("Party popularity", "Issue salience"),
values = c("solid", "dashed"))
ylab("Share (percentage point)")
scale_x_date(date_labels = "%b-%Y",
date_breaks = "5 month",
date_minor_breaks = "1 month",
expand = c(0, 0))
guides(x = guide_axis(n.dodge = 2))
theme_bw()
theme(legend.title= element_blank(),
legend.position="bottom",
legend.key.width = unit(2, "cm"),
axis.title.x = element_blank())