减少ggplot2中离散轴刻度之间的空间

时间:2017-07-01 09:34:49

标签: r ggplot2 data-visualization

我试图在ggplot2中绘制两个彼此平行的比例,如下所示。

data <- rbind(data.frame(type = "norm", value = 0:100, label = 0:100),
              data.frame(type = "trans", value = asin(0:100/100) *100, label = 0:100))

data <- data[data$label %% 1 == 0,]
data$size <- ifelse(data$label %% 10 == 0, 0.2,
                    ifelse(data$label %% 5 == 0, 0.1,
                           0.05))
data$R <- ifelse(data$type == "norm",
                 as.numeric(data$type) - data$size,
                 as.numeric(data$type))
data$L <- ifelse(data$type == "trans",
                 as.numeric(data$type) + data$size,
                 as.numeric(data$type))
data$Txt <- ifelse(data$type == "norm",
                   data$L - 0.3,
                   data$R + 0.3)

g <- ggplot(data, aes(x = type, y = value)) +
  geom_line(aes(group = type)) +
  scale_x_discrete("type", labels = parse(text = levels(data$type))) +
  annotate("segment", x = data$L, xend = data$R,
           y = data$value,
           yend = data$value) + 
  annotate("text", x = data[data$label %% 10 == 0,]$Txt,
           y = data[data$label %% 10 == 0,]$value,
           label = data[data$label %% 10 == 0,]$label) +
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_text(colour = "black",
                                   size = 12),
        plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

enter image description here

保留宽高比和尺寸,如何缩小normtrans尺度之间的空间以获得以下所需结果?enter image description here

1 个答案:

答案 0 :(得分:3)

expand=c(0,20)添加到scale_x_discrete并操纵其他输入。到目前为止代码看起来像:

data <- data[data$label %% 1 == 0,]
data$size <- ifelse(data$label %% 10 == 0, 4,
                    ifelse(data$label %% 5 == 0, 2,
                           1))
data$R <- ifelse(data$type == "norm",
                 as.numeric(data$type) - data$size,
                 as.numeric(data$type))
data$L <- ifelse(data$type == "trans",
                 as.numeric(data$type) + data$size,
                 as.numeric(data$type))
data$Txt <- ifelse(data$type == "norm",
                   data$L - 6,
                   data$R + 6)

g <- ggplot(data, aes(x = type, y = value)) +
  geom_line(aes(group = type)) +
  scale_x_discrete("type",
                   expand=c(0,20)) +
  annotate("segment", x = data$L, xend = data$R,
           y = data$value,
           yend = data$value) + 
  annotate("text", x = data[data$label %% 10 == 0,]$Txt,
           y = data[data$label %% 10 == 0,]$value,
           label = data[data$label %% 10 == 0,]$label) +
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_blank(),
        plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
g

enter image description here