R中的密度图函数有什么问题
我编译了一个脚本(如下所示)来创建我想要的所有17个高度列的绘图,但运行它时出现了一个错误。我在excel中创建了列,然后复制并粘贴到脚本中。我创建列的方式有错误吗R中的密度图函数有什么问题,r,R,我编译了一个脚本(如下所示)来创建我想要的所有17个高度列的绘图,但运行它时出现了一个错误。我在excel中创建了列,然后复制并粘贴到脚本中。我创建列的方式有错误吗 df <- read.table(text = "Height Height Height Height Height Height Height Height Height Height Height Height Height Height Height Height Height 1
df <- read.table(text = "Height Height Height Height Height Height Height Height Height Height Height Height Height Height Height Height Height
147 800 1500 3077 4299 5704 7361 9388 10610 12051 13855 16354 18549 20643 23860 26471 31004
141 797 1503 3096 4325 5733 7388 9404 10616 12051 13853 16352 18552 20649 23891 26504 31055
148 806 1515 3112 4342 5753 7409 9425 10638 12072 13874 16374 18572 20670 23896 26505 31050
145 808 1525 3136 4369 5779 7435 9449 10661 12096 13903 16418 18624 20710 23916 26511 31048
146 807 1523 3143 4390 5815 7489 9527 10751 12191 13984 16451 18613 20677 23871 26462 30989
151 807 1512 3101 4327 5730 7380 9392 10604 12037 13843 16361 18572 20672 23897 26499 31031
150 808 1517 3119 4355 5772 7439 9469 10689 12123 13914 16396 18581 20670 23901 26528 31097
136 798 1515 3127 4365 5783 7448 9474 10693 12136 13949 16449 18638 20723 23943 26548 31086
128 789 1502 3108 4345 5759 7423 9450 10672 12116 13925 16419 18605 20684 23886 26477 30989
144 805 1517 3122 4355 5763 7418 9432 10645 12081 13886 16385 18565 20640 23838 26428 30926
129 786 1492 3085 4315 5725 7384 9406 10624 12064 13871 16379 18581 20676 23894 26486 30983
142 802 1513 3124 4369 5793 7467 9502 10720 12150 13940 16422 18598 20678 23895 26501 31041
141 797 1504 3098 4327 5736 7394 9417 10637 12081 13891 16396 18590 20674 23883 26479 31005
139 797 1503 3097 4329 5742 7405 9432 10653 12095 13900 16391 18571 20654 23873 26482 30989
144 800 1504 3093 4320 5727 7385 9406 10623 12064 13882 16404 18609 20703 23918 26515 31012
152 813 1527 3143 4388 5810 7482 9516 10737 12173 13965 16423 18583 20649 23847 26450 30973
126 783 1489 3081 4306 5708 7356 9361 10573 12013 13830 16340 18542 20633 23850 26457 30975
142 803 1517 3131 4374 5795 7468 9504 10726 12163 13960 16437 18613 20686 23886 26482 30989
135 793 1502 3103 4333 5737 7387 9395 10604 12034 13837 16343 18538 20619 23819 26429 30977
139 801 1516 3133 4378 5804 7480 9520 10748 12194 13997 16471 18629 20689 23864 26431 30888
115 772 1479 3068 4293 5694 7341 9348 10562 12006 13831 16356 18563 20654 23861 26460 30964
140 797 1504 3101 4334 5748 7413 9440 10659 12100 13917 16422 18614 20701 23916 26491 30963
137 795 1504 3104 4336 5746 7401 9414 10625 12058 13863 16377 18577 20661 23860 26453 30967
146 802 1505 3095 4323 5732 7391 9414 10635 12089 13920 16423 18603 20681 23881 26450 30915
160 819 1529 3133 4368 5784 7448 9474 10693 12131 13933 16424 18607 20684 23878 26465 30964
134 791 1497 3089 4318 5729 7387 9410 10629 12070 13881 16389 18582 20665 23872 26466 30972
142 801 1511 3109 4338 5746 7406 9431 10652 12098 13910 16414 18609 20692 23877 26449 30943
145 804 1514 3117 4353 5767 7430 9455 10675 12113 13913 16408 18596 20680 23876 26452 30938
140 800 1511 3114 4348 5760 7421 9447 10667 12106 13911 16399 18571 20634 23828 26427 30943
147 803 1510 3108 4340 5753 7412 9426 10642 12082 13895 16399 18582 20659 23861 26450 30942", header = TRUE)
densities <- lapply(df, density)
plot(densities[[1]], main = "height")
plot(densities[[2]], col="red")
plot(densities[[3]], col="blue")
plot(densities[[4]], col = "green")
plot(densities[[5]], col="orange")
plot(densities[[6]], col="yellow")
plot(densities[[7]], col = "cyan")
plot(densities[[8]], col="khaki")
plot(densities[[9]], col="purple")
plot(densities[[10]], col = "brown")
plot(densities[[11]], col="olive")
plot(densities[[12]], col="royalblue")
plot(densities[[13]], col = "pink")
plot(densities[[14]], col="seagreen")
plot(densities[[15]], col="plum")
plot(densities[[16]], col = "tan")
plot(densities[[17]], col="black")
df我运行您提供的代码没有问题,只是您的一个颜色名称不起作用(橄榄色):
及
因为错误消息表明对象密度
在您的情况下没有预期的尺寸。要回答Gunnerfan在聊天中的问题,下面介绍如何将数据融合并在ggplot2
中绘制
首先,“融化”只是指将数据从“宽”格式(在本例中,每列一个高度)转换为长格式(在本例中,一列包含所有高度,另一列标识高度所指的“人”)。熔化的反面是从长到宽的“铸造”<代码>ggplot2
通常更喜欢长(融化)格式的数据。如果您以前没有使用过R
,这在一开始可能会让人感到非常困惑。无论如何,这里有一些代码来融化和绘制数据
# Note: This code assumes you've loaded a wide-format data frame already
# (using read.table, read.csv, etc.) and called it "df".
# Load packages we'll need
library(reshape2)
library(ggplot2)
# melt data from wide to long format
df.m = melt(df)
# Plot, separate panel for each density
ggplot(df.m, aes(value)) +
geom_density() +
facet_wrap( ~ variable, scale="free_x")
# Plot, one panel with different colors for each density
ggplot(df.m, aes(value, colour=variable)) +
geom_density()
您是否在plot.xy(xy,type,…)中收到错误
错误:无效的颜色名称“olive”
?对我来说似乎是不言自明的。我不是R的用户,所以我不确定为什么会有错误。错误的一个例子是密度[[2]]中的这个错误:下标越界。当我复制并粘贴您的精确代码时,我没有得到那个错误。(下次你应该在帖子中分享错误消息)。你确定你是按这个顺序执行这些命令的吗?肯定是的。我将把错误图像放在问题中,为什么图像顶部的read.table行中没有空格或制表符?谢谢,我想这是从excel复制时使用的格式。所以,当你应用头部(df)时,它会打印出你想要的数字,我猜是多少?第一个Qu是否与第一个四分位数相同?没问题head(df)
打印数据帧的前5行。运行head(df)
的原因是为了查看您的数据是否与您期望的一样,或者是否缺少某些列,是否有坏的/缺少的数据等。您也可以执行View(df)
或tail(df)
来完成相同的任务。这是在我加载列之后吗?例如,在dft之后,我编写的代码将在您以问题中列出的格式(正确的格式,每列之间用空格或制表符分隔,而不是您随后发布的图像中的格式)将数据加载到数据框后运行。
class(densities)
head(densities)
# Note: This code assumes you've loaded a wide-format data frame already
# (using read.table, read.csv, etc.) and called it "df".
# Load packages we'll need
library(reshape2)
library(ggplot2)
# melt data from wide to long format
df.m = melt(df)
# Plot, separate panel for each density
ggplot(df.m, aes(value)) +
geom_density() +
facet_wrap( ~ variable, scale="free_x")
# Plot, one panel with different colors for each density
ggplot(df.m, aes(value, colour=variable)) +
geom_density()