1
votes

I'm trying to create a vertically oriented double plot with a line plot above and dot plot below, with both on the same (continuous, date) x-axis. I've successfully placed the two plots on a common axis and finished the (upper) line plot, but when I try to change the (lower) dot plot's x-axis from categorical to continuous, all my dots bunch up in the middle of the plot.

I only include here my code for the dot plot for simplicity, but if it turns out I need to show you the full double plot, I can do that.

Here's a small subset of my data, then my code, as far as I've gotten with it:

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)))

(My session info: R version 3.3.2 (2016-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1)

Any suggestions how I can (dot) plot this data on a continuous x-axis? (again, so I can line it up with the date axis in a plot above it)

1
scale_x_date is a continuous scale. Or do you mean you need to actually use scale_x_continuous?Axeman
Axeman: I don't know, but am open to suggestions. The code above does create a continuous scale, but doesn't plot the data in the way I'm aiming for.Joshua

1 Answers

0
votes

I'm not sure if this is what you are looking for, but let's see:

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) 

The trick here is to set the "group" argument in geom_dotplot to the dummy variable created before:

    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)))

giving:

enter image description here

Is this what you wanted ?