簡體   English   中英

我怎么能 bar_plot 這個data.frame?

[英]How could I bar_plot this data.frame?

vertejumi_ceturksnos <- data.frame(
  Vertejumi = c("0", "1", "2", "3"),
  Pirmais   = c(Pirmaiss),
  Otrais    = c(Otraiss),
  Tresais   = c(Tresaiss),
  Ceturtais = c(Pirmaiss)
)

產生的df 在此處輸入圖像描述

我想用這些值制作一個條形圖,所以 x 軸 <- 與 values("Pirmais","Otrais","Tresais","Ceturtais") <- 對應於一年中的季度和 y 軸進行比較<- 有條形達到表中的金額,金額對應於第一列中的 0:3 值

試試這個,如果您的數據格式與圖像相同並且我理解這個問題,應該可以工作。

library(tidyr)
library(ggplot2)
    
   


vertejumi_ceturksnos %>%
    pivot_longer(cols = -Vertejumi) %>%
    ggplot(aes(x = factor(name, 
                          levels = c("Primais", 
                                     "Otrais", 
                                     "Tresais", 
                                     "Ceturtais")),
               y = value, 
               fill = Vertejumi))+
    geom_bar(stat = "identity")+
    labs(x = "x label", y = "y label")

使用melt重塑數據如下所示:

library(tidyverse)
library(reshape)
vertejumi_ceturksnos %>%
  melt() %>%
  ggplot(aes(x = variable, y = value, fill = Vertejumi)) +
  geom_bar(stat = "identity")

輸出:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM