在离散x轴

时间:2017-04-19 15:19:22

标签: r ggplot2

我正在尝试创建一个用于发布的图形,并且我的字段中的图形约定对于此信息是非标准的(绘制听力图)。

一些示例数据:

subsetAudio <- 
structure(list(Subject = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L), Frequency = c(250, 250, 250, 500, 500, 500, 750, 
750, 750, 1000, 1000, 1000, 1500, 1500, 1500, 2000, 2000, 2000, 
3000, 3000, 3000, 4000, 4000, 4000, 6000, 6000, 6000, 8000, 8000, 
8000, 250, 250, 250, 500, 500, 500, 750, 750, 750, 1000, 1000, 
1000, 1500, 1500, 1500, 2000, 2000, 2000, 3000, 3000, 3000, 4000, 
4000, 4000, 6000, 6000, 6000, 8000, 8000, 8000), Ear = structure(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, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Left", 
"Right"), class = "factor"), dBHL = c(10L, 30L, 20L, 10L, 45L, 
20L, 25L, 50L, 20L, 30L, 45L, 25L, 45L, 45L, 60L, 50L, 45L, 60L, 
55L, 45L, 60L, 60L, 50L, 60L, 65L, 70L, 65L, 65L, 75L, 70L, 30L, 
35L, 25L, 10L, 45L, 25L, 30L, 50L, 25L, 30L, 50L, 30L, 40L, 45L, 
45L, 50L, 55L, 60L, 60L, 55L, 60L, 60L, 60L, 65L, 65L, 75L, 60L, 
65L, 85L, 65L)), .Names = c("Subject", "Frequency", "Ear", "dBHL"
), row.names = c(1L, 2L, 3L, 11L, 12L, 13L, 21L, 22L, 23L, 31L, 
32L, 33L, 41L, 42L, 43L, 51L, 52L, 53L, 61L, 62L, 63L, 71L, 72L, 
73L, 81L, 82L, 83L, 91L, 92L, 93L, 101L, 102L, 103L, 111L, 112L, 
113L, 121L, 122L, 123L, 131L, 132L, 133L, 141L, 142L, 143L, 151L, 
152L, 153L, 161L, 162L, 163L, 171L, 172L, 173L, 181L, 182L, 183L, 
191L, 192L, 193L), class = "data.frame")

令人困难的是,由于值的间距,“频率”必须作为一个因子输入,在离散的x标度上。

标记所有离散点:

ggplot(subsetAudio, aes(x = factor(Frequency), y = dBHL, group = Subject)) +
  theme_bw() +
  facet_grid(~ Ear) + 
  geom_line(linetype = "dashed",size=0.8) +
  scale_x_discrete(name = "Frequency (kHz)",labels=c("0.25", "0.5", "0.75", "1", "1.5", "2", "3", "4", "6", "8"), position = "top") +
    scale_y_reverse(name = "Level (dB HL)", lim=c(120,-10),minor_breaks = seq(0 ,120, 5), breaks = seq(-10, 120, 10)) +
  theme(panel.grid.major = element_line(colour = "black",size=0.1)) +
  theme(panel.grid.minor = element_line(colour = "black",linetype="dashed",size=0.1)) +
  theme(strip.text.x = element_blank()) + 
  geom_text(data=data.frame(x = 1.5, y = -6.25, label=c("a)", "b)"), 
                            Ear=c("Left","Right")), aes(x,y,label=label), size=8,inherit.aes=FALSE)

这产生了下图: enter image description here

这里的问题是我需要将0.75,1.5,3和6处的x比例网格线虚线化。

我尝试绘制“主要”网格线(.25,5,1,2,4,8):

ggplot(subsetAudio, aes(x = factor(Frequency), y = dBHL, group = Subject)) +
  theme_bw() +
  facet_grid(~ Ear) + 
  geom_line(linetype = "dashed",size=0.8) +
  scale_x_discrete(name = "Frequency (kHz)",breaks=c("250","500","1000","2000","4000","8000"),labels=c("0.25", "0.5", "1", "2", "4", "8"), position = "top") +
  scale_y_reverse(name = "Level (dB HL)", lim=c(120,-10),minor_breaks = seq(0 ,120, 5), breaks = seq(-10, 120, 10)) +
  theme(panel.grid.major = element_line(colour = "black",size=0.1)) +
  theme(panel.grid.minor = element_line(colour = "black",linetype="dashed",size=0.1)) +
  theme(strip.text.x = element_blank()) + 
  geom_text(data=data.frame(x = 1.5, y = -6.25, label=c("a)", "b)"), 
                            Ear=c("Left","Right")), aes(x,y,label=label), size=8,inherit.aes=FALSE)

这给出了这个情节: enter image description here

但是,我无法弄清楚如何在缺失点(0.75,1.5,3和6)添加虚线

关于我如何解决这个问题的任何建议?

最后,我需要在x轴上混合使用实线和虚线:

0.25 - solid
0.5 - solid
0.75 - dashed
1 - solid
1.5 - dashed
2 - solid
3 - dashed
4 - solid
6 - dashed
8 - solid

1 个答案:

答案 0 :(得分:1)

通过将“频率”变量转换为连续变量,我做了一些hacky进展:

subsetAudio$Frequency <- as.numeric(mapvalues(Avg_Audio$Frequency, c("250","500","750","1000","1500","2000","3000","4000","6000","8000"), 
                                     c("1","2","2.5","3","3.5","4","4.5","5","5.5","6")))

然后玩主要和次要网格线:

ggplot(Audios, aes(x = Frequency, y = dBHL, group = Subject)) +
  theme_bw() +
  facet_grid(~ Ear) + 
  geom_line(linetype = "dashed",size=0.8) +
  geom_line(data = Avg_Audio, aes(Frequency,dBHL, group = Subject), size = 1.5) + 
  scale_x_continuous(name = "Frequency (kHz)",breaks=c(1,2,3,4,5,6),labels=c("0.25", "0.5", "1", "2", "4", "8"),minor_breaks=c(2.5,3.5,4.5,5.5), position = "top") +
  scale_y_reverse(name = "Level (dB HL)", lim=c(120,-20),minor_breaks = seq(0 ,120, 10), breaks = seq(-20, 120, 20)) +
  theme(panel.grid.major = element_line(colour = "black",size=0.1), panel.grid.minor.x = element_line(colour="grey", linetype="dashed"),
        strip.text.x = element_blank()) +
  geom_text(data=data.frame(x = 1.5, y = -10, label=c("a)", "b)"), 
            Ear=c("Left","Right")),
            aes(x,y,label=label), size=8,inherit.aes=FALSE)

这会产生下图:enter image description here

这非常接近....但我找不到向次要(虚线)网格线(0.75,1.5,3,6)添加刻度线或标签的方法。

有什么建议吗?