首页 > 解决方案 > 如何将每个数据集填充为不同的颜色?

问题描述

如果你执行代码,颜色都是黑色的。我需要将它更改为不同的颜色,但我似乎无法让它工作。我尝试在网上找到解决方案,但没有太大帮助。

加载 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"))

任何帮助将不胜感激,谢谢。

标签: rggplot2

解决方案


您需要更改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"))

在此处输入图像描述


推荐阅读