library(viridis)
## Loading required package: viridisLite
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
library(wesanderson)

# a very basic scatter plot
m1 <- ggplot(data=iris) +
  aes(x=Sepal.Width,
      y=Sepal.Length,
      color=Species) +
      geom_point()
print(m1)   

# creating a pair plot, I really enjoy this one. 
m2 <- ggplot(iris) +
  aes(color = Species) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point(aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(aes(x = Sepal.Length, y = Petal.Width)) +
  geom_point(aes(x = Sepal.Width, y = Petal.Length)) +
  geom_point(aes(x = Sepal.Width, y = Petal.Width)) +
  geom_point(aes(x = Petal.Length, y = Petal.Width)) +
  facet_grid(. ~ Species)

print(m2)

# heres an interesting way to plot the scatter points over the box plot, as we did in class
m3 <- ggplot(iris) + 
  aes(x = Species, y = Sepal.Width) +
  geom_boxplot(fill = "thistle", outlier.shape = NA) + 
  geom_point(position = position_jitter(width = 0.1, height = 0.7), color = "grey60", size = 2)

print(m3)

#and to change the colors to be a bit more pallatable
m4 <- ggplot(iris) + 
  aes(x = Species, y = Sepal.Width, fill=Species) +
  geom_boxplot(outlier.shape = NA) + 
  geom_point(position = position_jitter(width = 0.1, height = 0.7), color = "black", size = 2)

print(m4)

#now I want to do the alpha transparency technique we learned in class 
v1 <- c(iris$Sepal.Width, iris$Petal.Width)  # Combine Sepal.Width and Petal.Width

# creating variable for treatment groups
lab <- rep(c("Sepal Width", "Petal Width"), c(nrow(iris), nrow(iris)))  

df <- data.frame(v1 = v1, lab = lab)

m5 <- ggplot(df) +
      aes(x = v1, fill = lab) +
      geom_histogram(position = "identity", alpha = 0.5, color = "black", bins = 20)  # Specify the number of bins

print(m5)

#ok so I am a big wes anderson fan, my two favorite movies are isle of dog and grand budapest hotel, I will make plots that include those. Having trouble with the traditional format of including aes as its own function on a separate line so getting it to work this way.
palette <- wes_palette("IsleofDogs1")

#isle of dog
m6 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette) +  
  labs( title = "Iris Sepal Length vs Sepal Width", x = "Sepal Length", y = "Sepal Width") +
  theme_minimal() +  
  theme( legend.position = "bottom")

print(m6)

#grand budapest hotel
palette2 <- wes_palette("GrandBudapest2")

m7 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette2) +  
  labs( title = "Iris Sepal Length vs Sepal Width", x = "Sepal Length", y = "Sepal Width") +
  theme_minimal() +  
  theme( legend.position = "bottom")

print(m7)

#to move onn to the portion of the homework that's asking to explore ggplot features we haven't used

#here, just for interest, I want to highlight the highest and lowest points on the graph. I will use the last m7 plot I just created for this. 

m7 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette2) +  
  labs(title = "Iris Sepal Length vs Sepal Width", x = "Sepal Length", y = "Sepal Width") +
  theme_minimal() +  
  theme(legend.position = "bottom")

# identifying highest and lowest points 
highest_points <- iris[which.max(iris$Sepal.Width), ]
lowest_points <- iris[which.min(iris$Sepal.Width), ]

# adding circles around highest and lowest points
m7 <- m7 +
  geom_point(data = highest_points, size = 5, shape = 1, color = "black") + 
  geom_point(data = lowest_points, size = 5, shape = 1, color = "black")   

print(m7)

#cool, that works, now I will add a caption
m7 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette2) +  
  labs(title = "Iris Sepal Length vs Sepal Width", x = "Sepal Length", y = "Sepal Width", caption="This is an example caption. Testing Testing 1212.") +
  theme_minimal() +  
  theme(legend.position = "bottom")

# identifying highest and lowest points 
highest_points <- iris[which.max(iris$Sepal.Width), ]
lowest_points <- iris[which.min(iris$Sepal.Width), ]

# adding circles around highest and lowest points
m7 <- m7 +
  geom_point(data = highest_points, size = 5, shape = 1, color = "black") + 
  geom_point(data = lowest_points, size = 5, shape = 1, color = "black")   

print(m7)

#I don't like that it's in the bottom right corner, I want to move it to the center. 
m7 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette2) +  
  labs(
    title = "Iris Sepal Length vs Sepal Width",
    x = "Sepal Length",
    y = "Sepal Width",
    caption = "This is an example caption."  # Add caption
  ) +
  theme_minimal() +  
  theme(
    legend.position = "bottom",
    plot.caption = element_text(hjust = 0.5)  # Center caption
  )
# identifying highest and lowest points 
highest_points <- iris[which.max(iris$Sepal.Width), ]
lowest_points <- iris[which.min(iris$Sepal.Width), ]

# adding circles around highest and lowest points
m7 <- m7 +
  geom_point(data = highest_points, size = 5, shape = 1, color = "black") + 
  geom_point(data = lowest_points, size = 5, shape = 1, color = "black")   

print(m7)

#ok just because I don't know how to do it, I want to learn how to add an arrow at a certain point. For funsies, I will make it a random point. 

m7 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette2) +  
  labs(
    title = "Iris Sepal Length vs Sepal Width",
    x = "Sepal Length",
    y = "Sepal Width",
    caption = "This is an example caption."  # Add caption
  ) +
  theme_minimal() +  
  theme(
    legend.position = "bottom",
    plot.caption = element_text(hjust = 0.5)  # Center caption
  )
# identifying highest and lowest points 
highest_points <- iris[which.max(iris$Sepal.Width), ]
lowest_points <- iris[which.min(iris$Sepal.Width), ]

#choosing random point
random <- iris[sample(nrow(iris), 1), ]

# adding arrow to random point
m7 <- m7 +
  geom_segment(
    aes(x = mean(range(iris$Sepal.Length)), y = mean(range(iris$Sepal.Width)),
        xend = Sepal.Length, yend = Sepal.Width),
    data = random,
    arrow = arrow(length = unit(0.3, "inches")),
    color = "black"
  )

print(m7)

#this will change everytime we run the code

m7 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(values = palette2) +  
  labs(
    title = "Iris Sepal Length vs Sepal Width",
    x = "Sepal Length",
    y = "Sepal Width",
    caption = "This is an example caption."  # Add caption
  ) +
  theme_minimal() +  
  theme(
    legend.position = "bottom",
    plot.caption = element_text(hjust = 0.5)  # Center caption
  )
# identifying highest and lowest points 
highest_points <- iris[which.max(iris$Sepal.Width), ]
lowest_points <- iris[which.min(iris$Sepal.Width), ]

#choosing random point
random <- iris[sample(nrow(iris), 1), ]

# adding arrow to random point
m7 <- m7 +
  geom_segment(
    aes(x = mean(range(iris$Sepal.Length)), y = mean(range(iris$Sepal.Width)),
        xend = Sepal.Length, yend = Sepal.Width),
    data = random,
    arrow = arrow(length = unit(0.3, "inches")),
    color = "black"
  )

print(m7)