首页 文章

无法映射到列名:ggplot2 geom_point graph

提问于
浏览
1

我在下面有一些数据

> x.row10
                                                      2003   2004   2005   2006   2007   2008   2009   2012
drift av bil                                         12770  12440  14910  12640  16230  16000  16260     NA
utemåltider                                           5700   6600   6050   7830   8870   7990   7580   9860
resor, hotell                                         5620   5820   7960   9600   8210   9390   9890     NA
möbler, inventarier, textilier , hushållsutrustning   8540  10840  13400   9450   9910  11860  10430  13040
hyra/avgift för hyres-/borätt (inkl garage)          43360  44020  45160  49430  45370  44090  48740     NA
köpta livsmedel                                      26420  27910  28160  29100  28310  33020  35910  33740
hushållstjänster                                      9490  11690  13770   8650   7250  10390  11490  17140
transport                                            27230  30810  28810  28410  30500  30390  29360  34890
bostad                                               67890  67250  71200  75210  71000  73490  74710  81820
fritid och kultur                                    34900  35860  43600  46770  43540  46160  45840  51000
totala utgifterna                                   215300 219870 241920 241060 229290 253590 255950 277260

我想创建一个ggplot,其中 colnames 映射到 x 变量, rownames 映射到 y 变量 . 基本上我只想分别在图表上绘制每年所有行名的数据点 . 这与一个显示不同行名的颜色aestethic因子图例一起 .

我的尝试

ggplot(x.row10, aes(x = colnames(x.row10), y = rownames(x.row10)) ) + geom_point()

但我明白了

错误:美学必须是长度为1或与dataProblems相同的长度:colnames(x.row10)

怎么解决这个问题?

1 回答

  • 3

    通常, ggplot 喜欢"long data",其中每一行都是一个度量,而不是将它们分组 . 要解决此问题,我们可以使用包 tidyr 使您的数据变长(或融化) . 您可能需要查看dat2,作为"long"或"melted"数据的示例 .

    library(dplyr)
    library(tidyr)
    
    dat2 <- x.row10 %>% mutate(type = row.names(.)) %>%
                        gather(year, val, -type) %>%
                        mutate(year = extract_numeric(year))
    
    ggplot(dat2, aes(x = year, y = val, colour = type)) + geom_line()
    

    这是输出:
    enter image description here

    由于你的数据难以进入,这就是我使用的(注意,如果你想要一个确切的代码,可能会有一些差异,尝试在你的问题中添加一个 dput(x.row10)

    structure(list(X2003 = c(12770L, 5700L, 5620L, 8540L, 43360L, 
    26420L, 9490L, 27230L, 67890L, 34900L, 215300L), X2004 = c(12440L, 
    6600L, 5820L, 10840L, 44020L, 27910L, 11690L, 30810L, 67250L, 
    35860L, 219870L), X2005 = c(14910L, 6050L, 7960L, 13400L, 45160L, 
    28160L, 13770L, 28810L, 71200L, 43600L, 241920L), X2006 = c(12640L, 
    7830L, 9600L, 9450L, 49430L, 29100L, 8650L, 28410L, 75210L, 46770L, 
    241060L), X2007 = c(16230L, 8870L, 8210L, 9910L, 45370L, 28310L, 
    7250L, 30500L, 71000L, 43540L, 229290L), X2008 = c(16000L, 7990L, 
    9390L, 11860L, 44090L, 33020L, 10390L, 30390L, 73490L, 46160L, 
    253590L), X2009 = c(16260L, 7580L, 9890L, 10430L, 48740L, 35910L, 
    11490L, 29360L, 74710L, 45840L, 255950L), X2012 = c(NA, 9860L, 
    NA, 13040L, NA, 33740L, 17140L, 34890L, 81820L, 51000L, 277260L
    )), .Names = c("X2003", "X2004", "X2005", "X2006", "X2007", "X2008", 
    "X2009", "X2012"), class = "data.frame", row.names = c("drift av bil", 
    "utemåltider", "resor, hotell", "möbler, inventarier, textilier , hushållsutrustning", 
    "hyra/avgift för hyres-/borätt (inkl garage)", "köpta livsmedel", 
    "hushållstjänster", "transport", "bostad", "fritid och kultur", 
    "totala utgifterna"))
    

相关问题