r - 如何将每个数据集填充为不同的颜色?
问题描述
如果你执行代码,颜色都是黑色的。我需要将它更改为不同的颜色,但我似乎无法让它工作。我尝试在网上找到解决方案,但没有太大帮助。
加载 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"))
任何帮助将不胜感激,谢谢。
解决方案
您需要更改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"))
推荐阅读
- javascript - 交换计算对象键javascript的顺序
- c++ - 双击“Laser On” 双击“Laser Off” - Arduino
- c - 如何在多线程环境中处理共享内存?
- r - 使用 R 示例中的 Replace 函数
- java - 如何在 Spring Boot 中使用真实数据库测试代码?
- c++ - C++-17:将函数指针转换为具有不同参数指针类型的函数
- websocket - Mqtt 和 Websocket 同时与 Aedes
- python - 确定包含特定条目的矩阵行子集的更快方法
- python - 导出到excel时将熊猫数据框中的列转换为数值
- c - 使用 exp 控制舍入是否存在错误?