控制 ggplot 山脊线图中的 X 轴值

问题描述

我在脊线密度图中叠加了密度图(男孩与女孩)。 X 轴值是从 1 到 9 的整数,但在密度图中,它们被平滑为从 0 到 10。此外,X 轴显示十进制值,例如 2.5 和 7.5。

  1. 有没有办法确保只显示整数值而不显示小数值?
  2. 我是否可以控制显示范围仅从 1 到 9 而没有 0 和 10 部分:换句话说,只显示值 1 和 9 之间的图形部分?

以下是一些示例数据:

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)

这是我的情节:

enter image description here

谢谢!

解决方法

尝试明确给出休息时间:

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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...