我想将geom_density
图添加到我的geom_point
中,并希望按年份间隔
就我而言,应该是2012年至2014年,2014年至2016年的密度...也许用facet_grid
?
我的脚本和数据:
list_of_packages <- c("dplyr", "ggplot2", "ggrepel")
new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)){
install.packages(new_packages)
}
lapply(list_of_packages, require, character.only = TRUE)
df %>%
ggplot(aes(x = reception_date, y = dura)) +
geom_point() +
theme_bw() +
geom_label_repel(aes(label = ifelse(duplicated_id == T, as.character(id), "")),
size = 3,
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50') +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title.x = element_text(hjust = 0.5, face = "bold"),
axis.title.y = element_text(hjust = 0.5, face = "bold"))
我的数据:
df <- structure(list(id = c(101L, 107L, 108L, 109L, 115L, 117L, 118L, 120L, 121L, 122L, 123L, 125L, 129L, 134L, 138L, 142L, 144L, 145L, 148L, 151L, 152L, 161L, 162L, 163L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 176L, 177L, 179L, 181L, 182L, 183L, 185L, 188L, 190L, 192L, 193L, 195L, 196L, 197L, 201L, 205L, 208L, 209L, 210L, 211L, 213L, 214L, 219L, 223L, 224L, 227L, 228L, 232L, 233L, 235L, 239L, 240L, 258L, 260L, 261L, 262L, 265L, 270L, 280L, 282L, 295L, 302L, 304L, 313L, 318L, 325L, 330L, 331L, 332L, 336L, 385L, 394L, 394L, 395L, 407L),
reception_date = structure(c(16332, 16846, 16658, 15295, 14257, 17519, 16261, 15553, 14208, 15559, 14880, 15714, 16268, 16476, 17708, 16528, 17564, 16861, 17375, 17512, 15364, 16233, 15651, 17582, 16835, 17562, 15244, 16041, 15184, 15783, 15309, 17577, 16504, 17686, 17491, 16203, 14620, 17207, 17592, 15582, 17631, 17185, 17007, 15204, 17017, 15996, 16451, 16779, 17462, 17029, 17618, 17513, 16617, 16315, 17207, 16654, 16520, 15894, 16623, 16365, 16853, 16498, 15426, 16846, 15504, 17619, 17395, 16637, 16982, 17340, 16717, 15895, 16833, 17207, 17021, 16000, 16101, 16342, 16335, 16076, 16668, 16392, 17154, 17115, 17599, 17599, 17115, 17448), class = "Date"),
year_reception_date = c(2014, 2016, 2015, 2011, 2009, 2017, 2014, 2012, 2008, 2012, 2010, 2013, 2014, 2015, 2018, 2015, 2018, 2016, 2017, 2017, 2012, 2014, 2012, 2018, 2016, 2018, 2011, 2013, 2011, 2013, 2011, 2018, 2015, 2018, 2017, 2014, 2010, 2017, 2018, 2012, 2018, 2017, 2016, 2011, 2016, 2013, 2015, 2015, 2017, 2016, 2018, 2017, 2015, 2014, 2017, 2015, 2015, 2013, 2015, 2014, 2016, 2015, 2012, 2016, 2012, 2018, 2017, 2015, 2016, 2017, 2015, 2013, 2016, 2017, 2016, 2013, 2014, 2014, 2014, 2014, 2015, 2014, 2016, 2016, 2018, 2018, 2016, 2017),
duplicated_id = c(FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE ),
dura = c(3823L, 4003L, 3815L, 2360L, 1200L, 4430L, 3143L, 2404L, 1059L, 2410L, 1731L, 2506L, 2938L, 3054L, 4194L, 3014L, 3991L, 3257L, 3771L, 3908L, 1699L, 2476L, 1894L, 3825L, 3048L, 3775L, 1730L, 2254L, 1397L, 1996L, 1522L, 3759L, 2686L, 3838L, 3611L, 2324L, 710L, 3297L, 3682L, 1643L, 3661L, 3185L, 3006L, 1173L, 2986L, 1965L, 2420L, 2718L, 3339L, 2906L, 3495L, 3390L, 2494L, 2131L, 3021L, 2408L, 2274L, 1618L, 2347L, 2061L, 2518L, 2163L, 1091L, 2481L, 1108L, 2766L, 2542L, 1784L, 2099L, 3552L, 1741L, 830L, 1768L, 2051L, 1741L, 691L, 730L, 942L, 904L, 584L, 1115L, 900L, 1632L, 1013L, 1407L, 1407L, 893L, 922L )),
class = c("grouped_df", "tbl_df", "tbl", "data.frame" ), row.names = c(NA, -88L), vars = "id", drop = TRUE, indices = list( 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84:85, 86L, 87L),
group_sizes = 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, 2L, 1L, 1L), biggest_group_size = 2L,
labels = structure(list( id = c(101L, 107L, 108L, 109L, 115L, 117L, 118L,120L, 121L, 122L, 123L, 125L, 129L, 134L, 138L, 142L, 144L, 145L, 148L, 151L, 152L, 161L, 162L, 163L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 176L, 177L, 179L, 181L, 182L, 183L, 185L, 188L, 190L, 192L, 193L, 195L, 196L, 197L, 201L, 205L, 208L, 209L, 210L, 211L, 213L, 214L, 219L, 223L, 224L, 227L, 228L, 232L, 233L, 235L, 239L, 240L, 258L, 260L, 261L, 262L, 265L, 270L, 280L, 282L, 295L, 302L, 304L, 313L, 318L, 325L, 330L, 331L, 332L, 336L, 385L, 394L, 395L, 407L)), class = "data.frame", row.names = c(NA, -87L), vars = "id", drop = TRUE))
答案 0 :(得分:1)
您是否考虑过使用ggridges
软件包?它不会像您绘制时那样在x轴上保留时间,但是使用您的数据我在概念上产生了非常相似的内容:
library(ggplot2)
# install.packages("ggridges")
library(ggridges)
library(dplyr)
df %>%
filter(year_reception_date > 2010) %>%
ggplot(aes(x = dura,
y = as.factor(year_reception_date))) +
geom_density_ridges(jittered_points = TRUE,
alpha = .7)
您还可以将coord_flip()
添加到绘图中,以获得与绘图更相似的内容。