簡體   English   中英

如何在ggplot中為X軸使用特定值的堆積條形圖?

[英]How to do a stacked bar chart with specific values for X axis in ggplot?

到目前為止,我一直在研究關於此的許多教程,但均未成功。 我有這個簡單的數據集。

TestCases     Column-1  Column-2 
TestCase-1        2       5     
TestCase-2        3       8
TestCase-3        4       7
TestCase-4        5       9
TestCase-5        2       7

我需要在ggplot中制作一個堆疊的直方圖,該直方圖結合了Column-1和Column-2的值,並在X軸上具有TestCases列的名稱,例如TestCase-1,TestCase2等。

您需要先使用tidyr::gather重塑數據,然后才能使用ggplot

df <- read.table(header = TRUE, text = "
  TestCases     Column-1  Column-2 
  TestCase-1        2       5     
  TestCase-2        3       8
  TestCase-3        4       7
  TestCase-4        5       9
  TestCase-5        2       7")

df2 <- tidyr::gather(df, key = "Column", value = "Values", -TestCases)
ggplot(df2, aes(x = TestCases, y = Values, fill = Column)) +
  geom_bar(stat = "Identity")

暫無
暫無

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

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