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

LPKirby

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.

Dominik S. Meier

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I set each character to a different color/background color in a JTextPane?

How do I set the animation color of a LinearProgressIndicator?

Set a different color for each tag

How do I set a different system property for each jmv gradle spawns when testing in parallel?

Matlab: How to set color of legend in a scatter plot where each data point gets a different color?

How to set different color for each active tab in Ionic

OpenGL - How do I fill a buffer with no data?

How do I color fill this EMA Chart

How can I fill an html table data cell with a color?

How do I change the color of each character

How do I set a different data source for the DataGridView using BindSource?

Arcgis in Python: Color change on each execution. How to set fill color?

How do I set up different pipelines for each branch in Azure

How to set different color for each items in RecyclerViews' GridLayoutManager

How do I set a different wallpaper for each workspace in Xubuntu 13.04?

How do I use fill color for negative values in spider chart?

How do I fill the current selection with color?

How do I use PROC EXPAND to fill in time series observations within a panel (longitudinal) data set?

how do I set this code so that each line has a different colour, dash and legend

How to set different color to each row

How do you set the fill color of a TextRange?

How do I fill an <hr> divider in CSS with a color?

How do I set color attribute each text line on HTML corresponding to javascript code line on execution?

How do I use a function to plot two different data set?

How do I define a different tooltip color per each serie on a line chart with google charts?

How do I set dynamically different font size for each item in series of Treemap chart of amchart4

How do I set a table with different amount of columns on each row?

How do I set a color for each point in a graph depending on a value

how do i set fill color of a svg circle with the help of thymeleaf