LPKirby
LPKirby

Reputation: 65

How do I fill each data set as a different color?

If you execute the code the colors are all black. I need to change it to different colors but I can't seem to get it to work. I've tried finding a solution online but it didn't help much.

Load r packages


library(ggplot2) #Import package for more customizable plots
library(reshape2) #Import package to melt dataframe

Sample data


#Determining how many bags of cat nip to buy for an dog animal show using the hypergeometric distribution.

populationSize = 1000 #Population of animals in a given city (Population Size)
numberOfCatsInTown = c(50, 150, 300) #Number of Cats in the town
numberOfAnimalsAtEvent = 250 #How many animals show up to the event (Sample Size)
x = seq(0, 80) #Probability of getting x number of cats attending the event 

# Calculate Probabilities for the PMF 

scenario1d = dhyper(x, numberOfCatsInTown[1], populationSize, numberOfAnimalsAtEvent) #hypergeometric PMF
scenario2d = dhyper(x, numberOfCatsInTown[2], populationSize, numberOfAnimalsAtEvent) #hypergeometric PMF
scenario3d = dhyper(x, numberOfCatsInTown[3], populationSize, numberOfAnimalsAtEvent) #hypergeometric PMF

# Calcualte the Probabilities for the CDF

scenario1p = phyper(x, numberOfCatsInTown[1], populationSize, numberOfAnimalsAtEvent) #hypergeometric CDF
scenario2p = phyper(x, numberOfCatsInTown[2], populationSize, numberOfAnimalsAtEvent) #hypergeometric CDF
scenario3p = phyper(x, numberOfCatsInTown[3], populationSize, numberOfAnimalsAtEvent) #hypergeometric CDF

#Create dataframes

myDataFrame = data.frame(x, scenario1d, scenario2d, scenario3d) #Creates a dataframe for the PMF
myDataFrame2 = data.frame(x, scenario1p, scenario2p, scenario3p) #Creates a dataframe for the CDF

#Melt the dataframes

data.m1 = melt(myDataFrame, id.vars = "x")
data.m2 = melt(myDataFrame2, id.vars = "x")





Plots


#Create a plot for the PMF 

ggplot(data.m1, aes(x = x, y = value, fill = variable)) +
      geom_point() + geom_line() +
      labs(x = "x", y = "Probability", title = "PMF Number of Cats at the Animal Show (N = 1000, n = 250)") +
      theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.9,0.8)) + 
      scale_fill_discrete(name = "M", labels = c("50", "150", "300")) + 


#Create a plot for the CDF

ggplot(data.m2, aes(x = x, y = value, fill = variable)) +
      geom_point() + geom_line() + 
      labs(x = "x", y = "Probability", title = "CDF Number of Cats at the Animal Show (N = 1000, n = 250)") + 
      theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.9, 0.2)) + 
      scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

Any help would be appreciated, thanks.

Upvotes: 1

Views: 191

Answers (1)

Ahorn
Ahorn

Reputation: 3876

You need to change fill to color in ggplots aes() call:

library(ggplot2)
ggplot(data.m1, aes(x = x, y = value, color = variable)) +
  geom_point() + geom_line() +
  labs(x = "x", y = "Probability", title = "PMF Number of Cats at the Animal Show (N = 1000, n = 250)") +
  theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.9,0.8)) +
  scale_color_discrete(name = "M", labels = c("50", "150", "300"))  


  #Create a plot for the CDF

  ggplot(data.m2, aes(x = x, y = value, color = variable)) +
  geom_point() + geom_line() + 
  labs(x = "x", y = "Probability", title = "CDF Number of Cats at the Animal Show (N = 1000, n = 250)") + 
  theme(plot.title = element_text(hjust = 0.5), legend.position = c(0.9, 0.2)) +
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"), labels = c("50", "150", "300"))

enter image description here

Upvotes: 3

Related Questions