[英]How to do a stacked bar chart with specific values for X axis in ggplot?
I have been looking into many tutorials about this without success so far. 到目前为止,我一直在研究关于此的许多教程,但均未成功。 I have this simple dataset.
我有这个简单的数据集。
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
I need to make, in ggplot, a stacked histogram combining the values of Column-1 and Column-2 and having in the X axis the names of the TestCases column, eg, TestCase-1, TestCase2, etc. 我需要在ggplot中制作一个堆叠的直方图,该直方图结合了Column-1和Column-2的值,并在X轴上具有TestCases列的名称,例如TestCase-1,TestCase2等。
You need to reshape your data first with tidyr::gather
, then you can plot with ggplot
. 您需要先使用
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.