两个变量并排条形图分类数据的ggplot

时间:2017-09-27 06:17:05

标签: r ggplot2 bar-chart

demo <- read.table(header = TRUE,
               text ="var1 Var2
               Good Excellent
               Subpar Good
               Excellent Decent
               Good Good
               Subpar Subpar")

如何创建并排条形图这些Var1和Var2,其中Y轴是每个不同值的计数?

例如,一个条形图将var1中的商品数量与var2进行比较?

2 个答案:

答案 0 :(得分:1)

希望这有帮助!

    
    {
        "name": "laravel/framework",
        "version": "v5.3.31",
        "source": {
            "type": "git",
            "url": "https://github.com/laravel/framework.git",
            "reference": "e641e75fc5b26ad0ba8c19b7e83b08cad1d03b89"
        },
        "dist": {
            "type": "zip",
            "url": "https://api.github.com/repos/laravel/framework/zipball/e641e75fc5b26ad0ba8c19b7e83b08cad1d03b89",
            "reference": "e641e75fc5b26ad0ba8c19b7e83b08cad1d03b89",
            "shasum": ""
        },
        "require": {
            "classpreloader/classpreloader": "~3.0",
            "doctrine/inflector": "~1.0",
            "ext-mbstring": "*",
            "ext-openssl": "*",
            "jeremeamia/superclosure": "~2.2",
            "league/flysystem": "~1.0",
            "monolog/monolog": "~1.11",
            "mtdowling/cron-expression": "~1.0",
            "nesbot/carbon": "~1.20",
            "paragonie/random_compat": "~1.4|~2.0",
            "php": ">=5.6.4",
            "psy/psysh": "0.7.*|0.8.*",
            "ramsey/uuid": "~3.0",
            "swiftmailer/swiftmailer": "~5.4",
            "symfony/console": "3.1.*",
            "symfony/debug": "3.1.*",
            "symfony/finder": "3.1.*",
            "symfony/http-foundation": "3.1.*",
            "symfony/http-kernel": "3.1.*",
            "symfony/process": "3.1.*",
            "symfony/routing": "3.1.*",
            "symfony/translation": "3.1.*",
            "symfony/var-dumper": "3.1.*",
            "vlucas/phpdotenv": "~2.2"
        },
        "replace": {
            "illuminate/auth": "self.version",
            "illuminate/broadcasting": "self.version",
            "illuminate/bus": "self.version",
            "illuminate/cache": "self.version",
            "illuminate/config": "self.version",
            "illuminate/console": "self.version",
            "illuminate/container": "self.version",
            "illuminate/contracts": "self.version",
            "illuminate/cookie": "self.version",
            "illuminate/database": "self.version",
            "illuminate/encryption": "self.version",
            "illuminate/events": "self.version",
            "illuminate/exception": "self.version",
            "illuminate/filesystem": "self.version",
            "illuminate/hashing": "self.version",
            "illuminate/http": "self.version",
            "illuminate/log": "self.version",
            "illuminate/mail": "self.version",
            "illuminate/notifications": "self.version",
            "illuminate/pagination": "self.version",
            "illuminate/pipeline": "self.version",
            "illuminate/queue": "self.version",
            "illuminate/redis": "self.version",
            "illuminate/routing": "self.version",
            "illuminate/session": "self.version",
            "illuminate/support": "self.version",
            "illuminate/translation": "self.version",
            "illuminate/validation": "self.version",
            "illuminate/view": "self.version",
            "tightenco/collect": "self.version"
        },
        "require-dev": {
            "aws/aws-sdk-php": "~3.0",
            "mockery/mockery": "~0.9.4",
            "pda/pheanstalk": "~3.0",
            "phpunit/phpunit": "~5.4",
            "predis/predis": "~1.0",
            "symfony/css-selector": "3.1.*",
            "symfony/dom-crawler": "3.1.*"
        },
        "suggest": {
            "aws/aws-sdk-php": "Required to use the SQS queue driver and SES mail driver (~3.0).",
            "doctrine/dbal": "Required to rename columns and drop SQLite columns (~2.4).",
            "fzaninotto/faker": "Required to use the eloquent factory builder (~1.4).",
            "guzzlehttp/guzzle": "Required to use the Mailgun and Mandrill mail drivers and the ping methods on schedules (~5.3|~6.0).",
            "league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (~1.0).",
            "league/flysystem-rackspace": "Required to use the Flysystem Rackspace driver (~1.0).",
            "pda/pheanstalk": "Required to use the beanstalk queue driver (~3.0).",
            "predis/predis": "Required to use the redis cache and queue drivers (~1.0).",
            "pusher/pusher-php-server": "Required to use the Pusher broadcast driver (~2.0).",
            "symfony/css-selector": "Required to use some of the crawler integration testing tools (3.1.*).",
            "symfony/dom-crawler": "Required to use most of the crawler integration testing tools (3.1.*).",
            "symfony/psr-http-message-bridge": "Required to use psr7 bridging features (0.2.*)."
        },
        "type": "library",
        "extra": {
            "branch-alias": {
                "dev-master": "5.3-dev"
            }
        },

Final Plot

答案 1 :(得分:0)

tidyverse非常适合:

library(tidyverse)
demo %>% 
  gather(key, value) %>% 
  mutate(value_ordered = factor(value, levels=c("Decent","Good", "Subpar", "Excellent"))) %>% 
  ggplot(aes(value_ordered, fill=key)) +
           geom_bar(position="dodge")

enter image description here

或宽度相同的条形码:

as.tbl(demo) %>% 
  gather(key, value) %>% 
  group_by(key, value) %>%   # group
  count() %>%    # count the frequency
  ungroup() %>%  # ungroup
  complete(key, value) %>%  # Complete missing combinations 
  mutate(value_ordered = factor(value, levels=c("Decent","Good", "Subpar", "Excellent"))) %>% 
  ggplot(aes(value_ordered,n, fill=key)) +
    geom_col(position = "dodge")   # it is recommended to use geom_col directly instead of stat="identity"