我有以下代码和后续图形。我如何:
1)使我的PY轴包含所有PY(即包括2009、2011、2013、2015和2017)
2)升序排列(即2009年位于顶部,而2018年位于底部
3)更改我的Perc轴的比例,也许是.2而不是.25,并可能添加更多网格线
数据:
structure(list(PY = structure(c(1230768000, 1262304000, 1293840000,
1325376000, 1356998400, 1388534400, 1420070400, 1451606400, 1483228800,
1514764800, 1546300800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Perc = c(0.197, 0.266, 0.2, 0.3, 0.397, 0.553, 0.57, 0.621,
0.556, 0.6, 0.293)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
代码:
ggplot(Ult_sum, aes(x=PY, y=Perc)) +
geom_segment( aes(x=PY, xend=PY, y=0, yend=1.0), color="skyblue") +
geom_point( color="blue", size=4, alpha=0.6) +
# theme_light() +
coord_flip() +
theme(
panel.grid.major.y = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank()
)
图:
作为奖励,我还收到以下警告/错误:
Warning messages:
1: Removed 1 rows containing missing values (geom_segment).
2: Removed 1 rows containing missing values (geom_point).
这似乎无缘无故删除我的2019年度报告。有想法吗?
答案 0 :(得分:0)
尝试一下。第一图重复了问题中给出的代码。第二个图是调整后的图:
library(ggplot2)
library(dplyr)
library(forcats)
library(stringr)
# data
Ult_sum <- structure(list(PY = structure(c(1230768000, 1262304000, 1293840000,
1325376000, 1356998400, 1388534400, 1420070400, 1451606400, 1483228800,
1514764800, 1546300800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Perc = c(0.197, 0.266, 0.2, 0.3, 0.397, 0.553, 0.57, 0.621,
0.556, 0.6, 0.293)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
# code given
ggplot(Ult_sum, aes(x=PY, y=Perc)) +
geom_segment( aes(x=PY, xend=PY, y=0, yend=1.0), color="skyblue") +
geom_point( color="blue", size=4, alpha=0.6) +
# theme_light() +
coord_flip() +
theme(
panel.grid.major.y = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank()
)
# Adjust plot
Ult_sum <- Ult_sum %>%
mutate(PY = as.Date(PY),
PY = factor(PY),
PY = fct_rev(PY))
ggplot(Ult_sum, aes(x=PY, y=Perc)) +
geom_segment( aes(x=PY, xend=PY, y=0, yend=1.0), color="skyblue") +
geom_point(color="blue", size=4, alpha=0.6) +
scale_x_discrete(labels = function(x) str_extract(x, "^\\d{4}")) +
scale_y_continuous(breaks = seq(0, 1, by = .2), minor_breaks = seq(0, 1, .05)) +
coord_flip() +
theme(
panel.border = element_blank(),
axis.ticks.y = element_blank()
)
由reprex package(v0.3.0)于2020-03-15创建