我使用代码
在ggplot2中创建了一个堆积条形图shared_private_1k <- read.table("my_data_file_name", header=TRUE)
p <- ggplot(shared_private_1k, aes(x=popu,y=shared_SNVs,fill=type)) +
geom_bar(stat="identity", width = 0.5) + labs(x="Populations", y="% Shared and Private")
图表如下所示:
我想在每个shared portion
中添加小蓝条(例如,我已经通过图像编辑器在人口1中添加了以显示我需要的内容)。该小蓝条的长度表示实际小于shared
值的值。所以它将落在shared
栏内
谢谢你的帮助。
答案 0 :(得分:3)
I believe you are just looking to add a second smaller width bar overlaying your stacked bars. You could try something like this, which I illustrated her with the mtcars dataset.
mtcars$shared <- mtcars$mpg*.4 # create a dummy variable representing shared
ggplot(mtcars) +
geom_bar(aes(x=as.character(cyl),y=mpg,fill=gear),stat="identity", width = 0.5) +
geom_bar(aes(x=as.character(cyl),y=shared),stat="identity", width = 0.2, fill="blue") +
labs(x="Cylinder", y="Miles / Gallon")
答案 1 :(得分:0)
如果Wyldsoul
的y值来自同一数据集,Axeman
和blue bars
分别编写和编辑的代码仍然有效。但在这种情况下,这些来自另一个日期文件,所以这是通过在第二个geom_bar()函数中定义data=my_data_file2
来解决的,该函数覆盖了前面的条形图。所以,最终的代码是:
p <- ggplot(shared_private_1k) + geom_bar(aes(x=popu,y=shared_SNVs,fill=type), stat="identity", width = 0.5)
p + geom_bar(data=my_data_file2, aes(x=popu, y=shared_multiply_by_proportion), stat="identity", width=0.1, fill="blue")