我试图将雌鱼和雄鱼总长度的直方图绘制为单独的向量。我尝试用自己的数据复制其他示例,但它不适合我。
我的载体长度不同,所以我给男性添加了NA并尝试绑定它们。
#Make data frames
Female<-data.frame(1812, 1393, 915, 1863, 1263, 2140, 2125, 1195, 865, 1350, 1320, 1405, 1670, 1720, 1340, 1660, 1915, 1940, 1720, 1455, 1499, 2071, 1955, 2270, 1592, 2260, 2500, 1905, 874, 1684, 1842, 1702, 1873, 2234, 1876, 1787, 976, 1022, 1062, 1476, 1444, 1445, 2380, 1545, 1512, 1840, 2002, 1427, 1450, 1622, 1402, 1408, 1495, 1416, 1980, 1270, 2540, 1232, 1809)
Male<-data.frame(1470, 1596, 2332, 2049, 2103, 1555, 1760, 1123, 1059, 1601, 1370, 1805, 1295, 1313, 1405, 1870, 1765, 1515, 1292, 1730, 1647, 1931, 2255, 2130, 1735, 1460, 2188, 1121, 1121, 1940, 1722, 1530, 1530, 1553, 908, 1434, 1600, 1742, 1990, 1550, 1422, 2094, 1333, 1555, 1774, 1911, 1651, 1476, 1360, 1830, 1744, 1780, 790, 1046, 740, NA, NA, NA, NA)
#Combine data frames
Female$Sex<-'Female'
Male$Sex<-'Male'
#combine into new dataframe Sexes
Sexes<-rbind(Female$Sex, Male$Sex)
现在我被卡住了。我不完全确定如何将其合并到ggplot函数中(这是我在R上的第三天!)理想情况下我想创建一个Interleaved图。任何帮助将非常感激。
答案 0 :(得分:1)
你的前两行有问题。 data.frame
期望一个向量,它将成为一个列。你给了它一堆数字,这些数字都将成为他们自己的专栏。通过使用c()
命令并为列命名以便稍后使用它来解决此问题。
Female<-data.frame(mass = c(1812, 1393, 915, 1863, 1263, 2140, 2125, 1195, 865, 1350, 1320, 1405, 1670, 1720, 1340, 1660, 1915, 1940, 1720, 1455, 1499, 2071, 1955, 2270, 1592, 2260, 2500, 1905, 874, 1684, 1842, 1702, 1873, 2234, 1876, 1787, 976, 1022, 1062, 1476, 1444, 1445, 2380, 1545, 1512, 1840, 2002, 1427, 1450, 1622, 1402, 1408, 1495, 1416, 1980, 1270, 2540, 1232, 1809))
Male<-data.frame(mass = c(1470, 1596, 2332, 2049, 2103, 1555, 1760, 1123, 1059, 1601, 1370, 1805, 1295, 1313, 1405, 1870, 1765, 1515, 1292, 1730, 1647, 1931, 2255, 2130, 1735, 1460, 2188, 1121, 1121, 1940, 1722, 1530, 1530, 1553, 908, 1434, 1600, 1742, 1990, 1550, 1422, 2094, 1333, 1555, 1774, 1911, 1651, 1476, 1360, 1830, 1744, 1780, 790, 1046, 740, NA, NA, NA, NA))
#Combine data frames
Female$Sex<-'Female'
Male$Sex<-'Male'
你对rbind
data.frames的命令只是采用性别列而不是整个data.frame。通过删除$Sex
#combine into new dataframe Sexes
Sexes<-rbind(Female, Male)
使用ggplot
,只需提供数据并告诉它你想要的美学效果。
library(ggplot2)
ggplot(Sexes, aes(x = mass, fill = Sex)) +
geom_histogram()
答案 1 :(得分:-1)
您需要的是将其作为一个长数据表
Female<-c(1812, 1393, 915, 1863, 1263, 2140, 2125, 1195, 865, 1350, 1320, 1405, 1670, 1720, 1340, 1660, 1915, 1940, 1720, 1455, 1499, 2071, 1955, 2270, 1592, 2260, 2500, 1905, 874, 1684, 1842, 1702, 1873, 2234, 1876, 1787, 976, 1022, 1062, 1476, 1444, 1445, 2380, 1545, 1512, 1840, 2002, 1427, 1450, 1622, 1402, 1408, 1495, 1416, 1980, 1270, 2540, 1232, 1809)
Male<-c(1470, 1596, 2332, 2049, 2103, 1555, 1760, 1123, 1059, 1601, 1370, 1805, 1295, 1313, 1405, 1870, 1765, 1515, 1292, 1730, 1647, 1931, 2255, 2130, 1735, 1460, 2188, 1121, 1121, 1940, 1722, 1530, 1530, 1553, 908, 1434, 1600, 1742, 1990, 1550, 1422, 2094, 1333, 1555, 1774, 1911, 1651, 1476, 1360, 1830, 1744, 1780, 790, 1046, 740, NA, NA, NA, NA)
dat <- data.frame(value = c(Female, Male),
sex = c(rep("Female", length(Female)),
rep("Male", length(Male))))
这可以让你复制ggplot2
的例子