Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 无法绘制多个几何频率多边形_R_Ggplot2_Graph - Fatal编程技术网

R 无法绘制多个几何频率多边形

R 无法绘制多个几何频率多边形,r,ggplot2,graph,R,Ggplot2,Graph,我的数据是 PC_Name Electors_2009 Electors_2014 Electors_2019 Voters_2009 Voters_2014 1 Amritsar 1241099 1477262 1507875 814503 1007196 2 Anandpur Sahib 1338596 1564721 1698876 904606

我的数据是

          PC_Name Electors_2009 Electors_2014 Electors_2019 Voters_2009 Voters_2014
1         Amritsar       1241099       1477262       1507875      814503     1007196
2   Anandpur Sahib       1338596       1564721       1698876      904606     1086563
3         Bhatinda       1336790       1525289       1621671     1048144     1176767
4         Faridkot       1288090       1455075       1541971      930521     1032107
5  Fatehgarh Sahib       1207556       1396957       1502861      838150     1030954
6         Ferozpur       1342488       1522111       1618419      956952     1105412
7        Gurdaspur       1318967       1500337       1595284      933323     1042699
8       Hoshiarpur       1299234       1485286       1597500      843123      961297
9        Jalandhar       1339842       1551497       1617018      899607     1040762
10   Khadoor Sahib       1340145       1563256       1638842      946690     1040518
11        Ludhiana       1309308       1561201       1683325      846277     1100457
12         Patiala       1344864       1580273       1739600      935959     1120933
13         Sangrur       1251401       1424743       1529432      931247     1099467
   Voters_2019
1       859513
2      1081727
3      1200810
4       974947
5       985948
6      1172033
7      1103887
8       990791
9      1018998
10     1046032
11     1046955
12     1177903
13     1105888


我已经写了代码

data <- read.csv(file = "Punjab data 3.csv")
data
library(ggplot2)
library(reshape2)
long <- reshape2::melt(data, id.vars = "PC_Name")
ggplot(long, aes(PC_Name, value, fill = variable)) + geom_freqpoly(stat="identity",binwidth = 500)

data听起来您对横轴上的
PC\u Name
和纵轴上的
value
(熔化后)感兴趣。也许你会对一个条形图感兴趣,并将选民和选民并排比较

根据@camille的建议,您可以将数据框的
变量
列分解为两列(一列为
选民
选民
,另一列为年份)。这将为绘图选项提供灵活性

以下是一些可能的开始:

  • 您可以按照自己的意愿对
    变量
    因子进行排序(例如,
    选民2009
    选民2009
    选民2014
    等,以进行比较)并使用
    几何图形条
  • 您可以使用
    facet\u wrap
    选民
    选民
    进行年度比较
  • 库(ggplot2)
    图书馆(E2)
    
    long Hi Damandeep,你能不能让你的数据加载更容易,看看这里的一些提示。我还想在重塑后将结果放在绘图问题上,以使问题
    最小化
    。此外,请澄清您希望绘图的外观。在每个轴上绘制什么?哪些变量的行?不用担心你的英语,很好。在将数据融合成一个长形状后,您可能希望将值类型分为选民和选民,然后再转换成一个宽形状,这样您就有一个选民栏和一个选民栏。或者,可以将其保持在熔化状态,并在打印前过滤其中一种。无论哪种方式,都应该有其他的SO帖子已经涵盖了其中的每一个steps@Bulat“我不知道那些命令dput等是如何工作的,你能给我演示一下我试过了,但没办法吗?”本,我刚刚编辑过
    library(ggplot2)
    library(reshape2)
    
    long <- reshape2::melt(data, id.vars = "PC_Name")
    
    # Split electors/voters from year into 2 columns
    long <- cbind(long, colsplit(long$variable, "_", c("type", "year")))
    
    # Change order of variable factor for comparisons
    long$variable <- factor(long$variable, levels = 
                              c("Electors_2009", "Voters_2009",
                                "Electors_2014", "Voters_2014",
                                "Electors_2019", "Voters_2019"))
    
    # Plot value vs. PC_Name using barplot (all years together)
    ggplot(long, aes(PC_Name, value, fill = variable)) +
      geom_bar(position = "dodge", stat = "identity")
    
    # Show example plot faceted by year
    ggplot(long, aes(PC_Name, value, fill = type)) +
      geom_bar(position = "dodge", stat = "identity") +
      facet_wrap(~year, ncol = 1)