如何将每个数据集填充为不同的颜色?

时间:2020-05-31 07:25:20

标签: r ggplot2

如果执行代码,则颜色全为黑色。我需要将其更改为不同的颜色,但似乎无法正常工作。我尝试过在线查找解决方案,但并没有太大帮助。

加载r个包


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

样本数据


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






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

谢谢您的任何帮助。

1 个答案:

答案 0 :(得分:3)

您需要在fill color通话中将ggplots更改为aes()

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

相关问题