问题描述
我在脊线密度图中叠加了密度图(男孩与女孩)。 X 轴值是从 1 到 9 的整数,但在密度图中,它们被平滑为从 0 到 10。此外,X 轴显示十进制值,例如 2.5 和 7.5。
以下是一些示例数据:
rank<-c(1,2,3,1,5,7,4,6,8,9,3)
gender<-c("M","M","F","F")
time<-c(1,3)
data<-data.frame(rank,gender,time)
这是我使用的代码:
ggplot(math_dat,aes(x = rank,y = time,color = gender,point_color = gender,fill = gender)) +
geom_density_ridges(
jittered_points = TRUE,scale = .95,rel_min_height = .01,point_shape = "|",point_size = 3,size = 0.25,position = position_points_jitter(height = 0)
) +
scale_y_discrete(expand = c(0,0)) +
scale_x_continuous(expand = c(0,0),name = "Rankings") +
scale_fill_manual(values = c("#33FFDE50","#00663350"),labels = c("Girls","Boys")) +
scale_color_manual(values = c("#33FFDE","#006633"),guide = "none") +
scale_discrete_manual("point_color",values = c("#33FFDE",guide = "none") +
coord_cartesian(clip = "off") +
guides(fill = guide_legend(
override.aes = list(
fill = c("#33FFDEA0","#006633A0"),color = NA,point_color = NA)
)
) +
ggtitle("Ranks over time") +
theme_ridges(center = TRUE)
解决方法
尝试明确给出休息时间:
ggplot(data,aes(x = rank,y = time,color = gender,point_color = gender,fill = gender)) +
geom_density_ridges(
jittered_points = TRUE,scale = .95,rel_min_height = .01,point_shape = "|",point_size = 3,size = 0.25,position = position_points_jitter(height = 0)
) +
scale_y_discrete(expand = c(0,0)) +
scale_x_continuous(expand = c(0,0),name = "Rankings",breaks = seq(0,10,by = 2)) + #give the break values here
scale_fill_manual(values = c("#33FFDE50","#00663350"),labels = c("Girls","Boys")) +
scale_color_manual(values = c("#33FFDE","#006633"),guide = "none") +
scale_discrete_manual("point_color",values = c("#33FFDE",guide = "none") +
coord_cartesian(clip = "off") +
guides(fill = guide_legend(
override.aes = list(
fill = c("#33FFDEA0","#006633A0"),color = NA,point_color = NA)
)
) +
ggtitle("Ranks over time") +
theme_ridges(center = TRUE)