是否有一个函数可以绘制在x轴上具有年份而在y轴上具有值的条形图?

时间:2020-08-20 09:20:06

标签: r bar-chart

我有以下数据:

#include <windows.h> /* for HANDLE type, and console functions */
#include <stdio.h> /* standard input/output */
#include <stdlib.h> /* included for rand */

#define WIDTH 30
#define HEIGHT 30
#define BOMBS 10

HANDLE wHnd; /* write (output) handle */
HANDLE rHnd; /* read (input handle */
int check(int a)
{
    if (a == 0 || a == WIDTH - 1 || a == HEIGHT - 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void SetGrid(int grid[WIDTH][HEIGHT])
{
    int bomb[2] = { abs(rand() % WIDTH),
                   abs(rand() % HEIGHT) };
    char t[100];
    for (int i = 0; i < BOMBS; i++)
    {
        while (grid[bomb[0]][bomb[1]] < -1)
        {
            bomb[0] = abs(rand() % WIDTH);
            bomb[1] = abs(rand() % HEIGHT);
        }

        grid[bomb[0]][bomb[1]] = -9;
        if (check(bomb[0]) || check(bomb[1])) grid[bomb[0]][bomb[1]] = -6;
        if (check(bomb[0]) && check(bomb[1])) grid[bomb[0]][bomb[1]] = -4;
        if (bomb[0] + 1 <= WIDTH - 1 && bomb[1] + 1 <= HEIGHT - 1)
        {
            grid[bomb[0] + 1][bomb[1] + 1]++;
        }
        if (bomb[0] + 1 <= WIDTH - 1)
        {
            grid[bomb[0] + 1][bomb[1]]++;
        }
        if (bomb[1] + 1 <= HEIGHT - 1)
        {
            grid[bomb[0]][bomb[1] + 1]++;

        }
        if (bomb[0] - 1 >= 0 && bomb[1] + 1 <= HEIGHT - 1)
        {
            grid[bomb[0] - 1][bomb[1] + 1]++;
        }
        if (bomb[1] - 1 >= 0)
        {
            grid[bomb[0]][bomb[1] - 1]++;

        }
        if (bomb[1] - 1 >= 0 && bomb[0] + 1 <= WIDTH - 1)
        {
            grid[bomb[0] + 1][bomb[1] - 1]++;

        }
        if (bomb[0] - 1 >= 0 && bomb[1] - 1 >= 0)
        {
            grid[bomb[0] - 1][bomb[1] - 1]++;
        }
        if (bomb[0] - 1 >= 0)
        {
            grid[bomb[0] - 1][bomb[1]]++;
        }
    }
}

int main(void)
{

    srand(time(0));

    SMALL_RECT windowSize = { 0, 0, WIDTH - 1, HEIGHT - 1 };

    COORD bufferSize = { WIDTH, HEIGHT };

    COORD characterBufferSize = { WIDTH, HEIGHT };
    COORD characterPosition = { 0, 0 };
    SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };

    CHAR_INFO consoleBuffer[WIDTH][HEIGHT];

    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
    rHnd = GetStdHandle(STD_INPUT_HANDLE);

    SetConsoleTitle("Our shiny new title!");

    SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
    SetConsoleScreenBufferSize(wHnd, bufferSize);

    int startGrid[WIDTH][HEIGHT] = { 0 };
    SetGrid(startGrid);

    for (int x = 0; x < WIDTH; ++x)
    {
        for (int y = 0; y < HEIGHT; ++y)
        {

            if (startGrid[x][y] > 0)
            {
                consoleBuffer[x][y].Char.AsciiChar = '0' + startGrid[x][y];
                consoleBuffer[x][y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
            }
            else
            {
                consoleBuffer[x][y].Char.AsciiChar = (unsigned char)111;
                consoleBuffer[x][y].Attributes = (startGrid[x][y] < 0 ? FOREGROUND_RED : FOREGROUND_BLUE) | FOREGROUND_INTENSITY;
            }

        }

    }
    WriteConsoleOutputA(wHnd, consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);
    printf("\n\n");

    getchar();
}

和代码

Year    A    B      C       D       E
Y2000   6.8  5.3    36.7    41      10.2
Y2001   2   10.6    40.3    37.8    9.3
Y2002   2.2  8.8    38      40.6    10.5
Y2003   2.3 14.2    41.6    33.8    8.1
Y2004   2.9  6.8    42.3    38.3    9.7
Y2005   5.5 11.9    39.1    43.4    NA
Y2006   6.4  8.6    32.4    41.1    11.4
Y2007   7.7 13.7    29.6    38.9    10
Y2008   9   10.4    36.5    38.8    5.2
Y2009   NA    NA    50.9    49.1    NA

我正在努力获得自己想要的情节。它应该类似于此问题中的图: Barplot customization

1 个答案:

答案 0 :(得分:0)

这是您获得与所链接的地标相似的地标的方式。 我保留了您在示例中设置的所有设置。

library(ggplot2)
library(tidyr)

Data <- tibble::tribble(
    ~Year,    ~A,    ~B,      ~C,       ~D,       ~E,
    "Y2000",   6.8,  5.3,    36.7,    41,      10.2,
    "Y2001",   2,   10.6,    40.3,    37.8,    9.3,
    "Y2002",   2.2,  8.8,    38,      40.6,    10.5,
    "Y2003",   2.3, 14.2,    41.6,    33.8,    8.1,
    "Y2004",   2.9,  6.8,    42.3,    38.3,    9.7,
    "Y2005",   5.5, 11.9,    39.1,    43.4,    NA,
    "Y2006",   6.4,  8.6,    32.4,    41.1,    11.4,
    "Y2007",   7.7, 13.7,    29.6,    38.9,    10,
    "Y2008",   9,   10.4,    36.5,    38.8,    5.2,
    "Y2009",   NA,    NA,    50.9,    49.1,    NA
)


Data %>%
    pivot_longer(-Year) %>% 
    
    ggplot(aes(x = Year, y = value, fill = name)) +
    geom_bar(stat = "identity", position = "dodge") +
    theme_bw() +
    scale_fill_brewer(palette = "Set1") + 
    theme(legend.position = "right", axis.text.x = element_text(angle = 90,vjust = 0.2))

enter image description here 我不知道您是否也要复制标签。