Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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 ggplot2条形图两个数据帧比较_R_Ggplot2_Bar Chart_Linechart - Fatal编程技术网

R ggplot2条形图两个数据帧比较

R ggplot2条形图两个数据帧比较,r,ggplot2,bar-chart,linechart,R,Ggplot2,Bar Chart,Linechart,我想在R中创建一个条形图,并比较两个不同data.frame中的相同值 我的数据是这样的 第一个DF: 2004 2005 2006 unit region 1 1500 1000 2000 X region1 2 1000 2500 2800 Y region1 3 2000 2050 1900 X region2 4 2200 2100 2000 Y region2 etc. 第二个DF:

我想在R中创建一个条形图,并比较两个不同data.frame中的相同值

我的数据是这样的

第一个DF:

      2004  2005  2006 unit region

   1  1500  1000  2000  X    region1
   2  1000  2500  2800  Y    region1
   3  2000  2050  1900  X    region2
   4  2200  2100  2000  Y    region2
etc.
第二个DF:

     2004  2005  2006 unit region

  1   5     10    12   PP   region1
  2   3     5     8    SS   region1
  3   8     12    11   PP   region2
  4   7     5     5    SS   region2
etc.
我想做的是视觉对比:

  • 条形图(集群)-区域1单元X与第二个DF单元PP年的相同区域1(2004年、2005年、2006年)
  • 折线图与上述数据相同
  • 条形图(聚集)-一组10个区域,其中单元Y与第二个表格单元SS中的10个区域相同。年份(2004、2005、2006) 我想要一个条形图(群集条形图)
  • 如果有人能帮助我,我将不胜感激,一整天都在努力,却无法前进

    谢谢

    2004 2005 2006单位区域
    
    2004  2005  2006 unit region
    1500  1000  2000  X    region1
    1000  2500  2800  Y    region1
    2000  2050  1900  X    region2
    2200  2100  2000  Y    region2
    
    df1 <- read.table(con <- file("clipboard"), header = T)
    
    2004  2005  2006 unit region
    5     10    12   PP   region1
    3     5     8    SS   region1
    8     12    11   PP   region2
    7     5     5    SS   region2
    
    df2 <- read.table(con <- file("clipboard"), header = T)
    
    # Barplot (clustered) - region1 unit X with the same region1 from second DF unit PP. Years (2004, 2005, 2006)
    df1$df <- 1
    df2$df <- 2
    
    require(reshape2)
    require(ggplot2)
    
    df <- rbind(df1, df2)
    df <- melt(df, id.vars=c("region", "unit", "df"))
    
    ggplot(df[(df$region=="region1" & df$df == 1) | (df$region == "region1" & df$unit == "PP"),], 
           aes(variable, value)) + 
      geom_bar(aes(fill = factor(df)), position = "dodge", stat="identity")
    
    
    # Line chart the same data as above 
    ggplot(df[(df$region=="region1" & df$df == 1) | (df$region == "region1" & df$unit == "PP"),], 
           aes(variable, value)) + 
      geom_line(aes(fill = factor(df)), stat="identity")
    
    # Barplot (clustered) - a set of 10 regions with unit Y with the same 10 regions from second table unit SS. Years (2004, 2005, 2006) I would like to have a barplot (clustered barplot).
    cat("For this one you'd need to provide a suitable example, as the current example has only 2 regions")
    
    1500 1000 2000 X区域1 1000 2500 2800 Y区域1 2000 2050 1900 X区域2 2200 2100 2000 Y地区2
    df1您至少应该在一个数据库中共享您的数据。同时展示你所做的尝试,并明确询问你在哪里陷入困境。这个问题有点像你现在给我们布置的作业。您可能应该首先将您的数据放入一个more中,以便更易于使用ggplot。谢谢!这对我来说太完美了!我不知道我必须转换df,现在更有意义了!