Lab Portion

setwd("C:/Users/Sam/Documents/DSCI_605/Module_7/")

library(tidyverse)
library(readxl)
library(ggplot2)
library(colorspace)
land = read_xlsx('C:/Users/Sam/Documents/DSCI_605/Module_7/Crop_Range_GOES0901_CountJday.xlsx')
key = 1*(223:244)
col = colorspace::diverge_hcl(length(key))
png(file ='C:/Users/Sam/Documents/DSCI_605/Module_7/Crop_test.png',
    width = 800, height = 500)

ggplot(data = land) + 
  geom_point(aes(x = gmt, y =FDCount, color= as.factor(jday))) +
  scale_x_continuous(breaks = seq(1, 24, 12), limits = c(1, 24)) +
  scale_y_continuous(breaks = seq(1, 50, 25), limits =  c(1, 50)) +
  scale_color_manual(values = col) +
  labs(color = 'Julianday') + labs(x = "Local Time") + labs(y = "Count") +
  facet_wrap(~jday, ncol = 3)
dev.off()
## png 
##   2
par(mfrow = c(2,1))
hist(land$FDCount, xlab = "FDCount")
plot(land$gmt, land$FDCount, xlab="GMT time", ylab="Count")

filter(land, jday%in%seq(235,243)) %>%
  ggplot() +
  geom_point(aes(x=gmt, y=FDCount)) +
  scale_color_manual(values=col) +
  scale_x_continuous(breaks=seq(1, 24, 11), limits = c(1, 24)) + 
  labs(color = 'Julianday') + labs(x = "Local Time") + labs(y = "Count") +
  facet_wrap(~jday, nrow = 2)

library(ggplot2)
library(gridExtra)
library(egg)
library(gtable)
library(grid)
library(lattice)
p <- qplot(1, 1)
p2 <- xyplot(1~1)
r <- rectGrob(gp = gpar(fill = "grey90"))
t <- textGrob("text")
grid.arrange(t, p, p2, r, ncol=2)

p1 <- qplot(mpg, wt, data = mtcars, colour = cyl)
p2 <- qplot(mpg, data = mtcars) + ggtitle("title")
p3 <- qplot(mpg, data = mtcars, geom = "dotplot")
p4 <- p1 + facet_wrap( ~ carb, nrow = 1) + 
  theme(legend.position = "none") +
  ggtitle("facetted plot")
grid.arrange(p1, p2, p3, p4, nrow = 2)

ggarrange(p1, p4, widths = c(3,2))

gs <- lapply(1:9, function(ii)
  grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii)))
grid.arrange(grobs=gs, ncol=4,
             top="top label", bottom="bottom\nlabel",
             left="left label", right="right label")
grid.rect(gp=gpar(fill=NA))

lay <- rbind(c(1,1,1,2,3),
             c(1,1,1,4,5),
             c(6,7,8,9,9))
grid.arrange(grobs = gs, layout_matrix = lay)

gs <- lapply(1:4, function(ii)
  grobTree(rectGrob(gp=gpar(fill=ii, alpha=0.5)), textGrob(ii)))
grid.arrange(
  grobs = gs,
  width = c(2, 1, 1),
  layout_matrix = rbind(c(1, 2, NA),
                        c(3, 3, 4)))

grid.arrange(r, t, p, p2,
             widths = c(2, 1, 1),
             layout_matrix = rbind(c(1, 2, NA),
                                   c(3, 3, 4)))

set.seed(123)
pl <- lapply(1:11, function(x)
             qplot(1:10, rnorm(10), main = paste("plot", x)))
ml <- marrangeGrob(pl, nrow = 2, ncol = 2)
ggsave('C:/Users/Sam/Documents/DSCI_605/Module_7/multipage.pdf', ml)
ml

# graphics.off()
g <- ggplotGrob(qplot(1, 1) +
                  theme(plot.background = element_rect(colour = "black")))
qplot(1:10, 1:10) + 
  annotation_custom(
    grob = g,
    xmin = 1,
    xmax = 5,
    ymin = 5,
    ymax = 10
    ) +
  annotation_custom(
    grob = rectGrob(gp = gpar(fill = "white")),
    xmin = 6,
    xmax = Inf,
    ymin = -Inf,
    ymax =5
  )

g <- ggplotGrob(qplot(1, 1) +
                  theme(plot.background = element_rect(colour = "black")) +
                  scale_x_continuous(breaks = seq(0, 1.5, 0.5), limits = c(0, 1.5)) +
                  scale_y_continuous(breaks = seq(0, 1.5, 0.5), limits = c(0, 1.5)))
mytheme <- gridExtra::ttheme_default(
  core = list(fg_params = list(cex = 0.5)),
  colhead = list(fg_params = list(cex = 0.5)),
  rowhead = list(fg_params = list(cex = 0.5)))
qplot(1:10, 1:10) + 
  annotation_custom(
    grob = g,
    xmin = 1,
    xmax = 5,
    ymin = 5,
    ymax = 10
  ) +
  annotation_custom(
    grob = rectGrob(gp = gpar(fill = "white")),
    xmin = 6,
    xmax = Inf,
    ymin = -Inf,
    ymax =5
  ) +
  annotation_custom(
    grob =  textGrob(
      "this footnote is right-justified",
      gp = gpar(fontface = 3, fotsize = 9),
      hjust = 1,
      x = 0.5,
      y = 0.05)
  )

library(ggplot2)
library(ggplotify)
library(grid)
library(vcd)

# data(Titanic)
# View(Titanic)
# class(Titanic)

p1 <- as.ggplot(~barplot(1:10)) +
  annotate("text", x = .6, y = .5,
           label = "Hello Base Plot", size = 5,
           color = 'firebrick', angle = 45)

p2 <- as.ggplot(expression(plot(rnorm(10))))
p3 <- as.ggplot(function() plot(sin))

p4 <- as.ggplot(~mosaic(Titanic, shade = TRUE, legend = FALSE))

p5 <- as.ggplot(densityplot(~mpg|cyl, data = mtcars))

library(cowplot)

library(colorspace)

col <- rainbow_hcl(3)
names(col) <- unique(iris$Species)

color <- col[iris$Species]
p6 <- as.ggplot(~plot(iris$Sepal.Length, iris$Sepal.Width, col = color, pch = 15))

p7 <-  ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(shape = 15) + scale_color_manual(values = col, name = "")

legend <- get_legend(p7)

library(ggimage)
p8 <- p6 + geom_subview(x = 7, y = .78, subview = legend)

p9 <- as.ggplot(~image(volcano))

ml <- plot_grid(p1, p2, p3, p4, p5, p6, p7, p8, p9, ncol = 3, labels = LETTERS[1:9])
pdf('C:/Users/Sam/Documents/DSCI_605/Module_7/Last1.pdf')
print(ml)
dev.off
## function (which = dev.cur()) 
## {
##     if (which == 1) 
##         stop("cannot shut down device 1 (the null device)")
##     .External(C_devoff, as.integer(which))
##     dev.cur()
## }
## <bytecode: 0x00000000139f46a0>
## <environment: namespace:grDevices>

Homework Portion

Please choose a build-in dataset (e.g. mpg in ggplot2) or some other datasets to have graph view like this.

library(help="datasets")
df <- Indometh
df$Subject <- as.character(df$Subject)
df$Subject <- as.factor(df$Subject)

You can have different plot types from this graph below. For example, you can have bubble plot, box plot, histogram, table, text, even some other plots you can do.

p1 <- qplot(time, conc, data = df, colour = Subject) + ggtitle("Scatter Plot")
p2 <- qplot(conc, data = df) + ggtitle("Histogram")
p3 <- qplot(conc, data = df, geom = "dotplot") + ggtitle("Dot Plot")
p4 <- p1 + facet_wrap( ~ Subject, nrow = 1) + 
  theme(legend.position = "none") +
  ggtitle("Facetted Plot")
p5 <- as.ggplot(densityplot(~time|Subject, data = df)) + ggtitle("Density Plot")
p6 <- qplot(time, data = df, color = Subject, geom = "boxplot") + ggtitle("Time Box Plot")
p7 <- qplot(conc, data = df, color = Subject, geom = "boxplot") + ggtitle("Concentration Box Plot")
p8 <- qplot(time, conc, data = df, colour = factor(Subject), geom = c("point", "smooth")) + ggtitle("Smoothed Line")
grid.arrange(p1, p2, p3, p4, ncol = 2, nrow = 2)

grid.arrange(p5, p6, p7, p8, ncol = 2, nrow = 2)

Please have some descriptions of the data you used and the variable you choose.

The data I chose shows pharmacokinetics of indometacin from 6 different subjects. There is a time series element to this data with the time column as well as a numeric vector showing plasma concentrations of the medicine being injected.

Why did you choose this data?

I chose this data in order to have a variety of information from variables. This set has time related information, factors, and a numeric vector.

What information do you want to tell the audience?

I wanted to show my use of multiple plots in one arrangement image. These were condensed to 2x2 plot images due to sizing issues. These can easily be fixed through exporting as .png images as used in the lab. Since this will be submitted as an rmarkdown, I adjusted to a normal 2x2 grid arrangement rather than 2x4 or 4x2.

Why multiple plots can be used for showing your information?

There are different ways of visualizing this data. Putting them all in one image can be helpful as you can look at everything collectively and not need to switch between visualizations. Some visualizations will also compliment other visualizations.

Please tell me what information you can get from the plot.

The Scatter plot shows that highest concentration was administered at the lowest time increments. The histogram simply displays count of concentration levels. The highest counts are that of the lowest concentration. The Dot plot shows the same as a histogram, but with dots instead. Faceted plot shows the same information as the scatter plot, but separates based on Subject. Density plot shows the density of time per subject. I tried against concentration and received similar density plots, although slightly skewed due to the difference in time and concentration. Each were lower than 10 so results were small and similar. Box plots for time and concentration are shown as well. Time is shown approximately evenly distributed which can imply injections were done at scheduled times. Concentrations shows a concentration above 1 will likely be around the max or an outlair compared to other concentration. Lastly smoothed line shows the same information as the scatterplot, but with lines showing the movement of each subject as and the overall range of possibilities among all points.

Please make sure all the graphs in your plot are professional as a single graph.

grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, ncol = 2, nrow = 4)

As shown here, the graphs are scrunched in this form. Since it was asked I included as this, although the above plots are much easier to read.