如何使用连续的x轴(ggplot2)制作点图?

时间:2017-03-21 16:37:36

标签: r ggplot2 plot

我试图创建一个垂直方向的双图,上面的线图和下面的点图,两者都在相同的(连续的,日期的)x轴上。我已成功将两个图放在一个公共轴上并完成(上)线图,但当我尝试将(下)点图的x轴从分类更改为连续时​​,我的所有点在情节中间聚集起来。

为了简单起见,我在这里只包含了我的点图代码,但如果事实证明我需要向你展示完整的双图,我可以做到。

这是我的数据的一小部分,然后是我的代码,就我所知:

data <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                               1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
                                               2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L
), .Label = c("11/11/2016", "12/16/2016", "12/2/2016", "12/23/2016"
), class = "factor"), factor = c(2L, 2L, 2L, 2L, 2L, 3L, 3L, 
                                 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
                                 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), temp = c(-19.85, -19.94, -20.77, -21.3, -21.71, -21.88, -22.03, 
            -22.74, -22.86, -18.88, -19.02, -19.22, -19.32, -19.32, -19.55, 
            -19.68, -20.23, -20.32, -21.37, -16.63, -19.01, -19.67, -20.47, 
            -21.14, -21.23, -23.01, -24.43, -24.61, -24.76, -15.9, -18.87, 
            -19.02, -19.16, -19.44, -19.62, -22.38, -24.37, -24.92, -26.9
)), .Names = c("date", "factor", "temp"), class = "data.frame", row.names = c(NA, 
                                                                              -39L))


library(ggplot2)
library(scales)


#format date and order date levels (the second line here gives me a warning, but seems to do what I want it to)..
data$date <- as.Date(data$date, "%m/%d/%Y")
data$date.chr <- factor(data$date, as.character(data$date))
data$date.chr <- as.Date(data$date.chr)


#now plot..
ggplot(data, aes(x = date.chr, fill = factor(factor), y = temp)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center', method = 'histodot', binwidth = 0.3, position=position_dodge(0.8)) +
  scale_x_date(date_breaks = "2 weeks", labels = date_format("%e %b"), limits = as.Date(c("2016-11-04","2016-12-23"))) +
  labs(title="", x="", y="response temp (°C)") +
  theme_minimal() +
  theme(axis.title.y = element_text(vjust=1)) +
  theme(legend.position="top") +
  guides(fill = guide_legend(override.aes = list(size=10)))

(我的会话信息: R版本3.3.2(2016-10-31) 平台:x86_64-w64-mingw32 / x64(64位) 运行于:Windows 7 x64(build 7601)Service Pack 1)

我是否可以(点)在连续的x轴上绘制这些数据? (再次,所以我可以在它上面的图中用日期轴排列)

1 个答案:

答案 0 :(得分:0)

我不确定这是否是你想要的,但让我们看看:

data$date <- as.Date(data$date, "%m/%d/%Y")
data$date.chr <- factor(data$date)
#create dummy variable to get both the position and "filling" right
data$datefact <- paste(data$factor,data$date.chr) 

这里的技巧是将geom_dotplot中的“group”参数设置为之前创建的虚拟变量:

    ggplot(data, aes(x = date, y = temp)) +
  # geom_point() + 
  geom_dotplot(aes(x = date, group = datefact, fill = factor(factor)),binaxis = 'y', 
               stackdir = 'center',
               method = 'histodot', 
               binwidth = 0.3)+
  scale_x_date(date_breaks = "2 weeks", labels = date_format("%e %b"), limits = as.Date(c("2016-11-04","2016-12-23"))) +
  labs(title="", x="", y="response temp (°C)") +
  theme_minimal() +
  theme(axis.title.y = element_text(vjust=1)) +
  theme(legend.position="top") +
  guides(fill = guide_legend(override.aes = list(size=10)))

,并提供:

enter image description here

这是你想要的吗?