Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中创建带有图案而不是颜色的堆栈条形图_R_Bar Chart_Stacked Chart - Fatal编程技术网

如何在R中创建带有图案而不是颜色的堆栈条形图

如何在R中创建带有图案而不是颜色的堆栈条形图,r,bar-chart,stacked-chart,R,Bar Chart,Stacked Chart,我想创建一个堆叠条形图,如quick R中的代码所示: # Stacked Bar Plot with Colors and Legend counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts)) #带颜

我想创建一个堆叠条形图,如quick R中的代码所示:

# Stacked Bar Plot with Colors and Legend
counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
xlab="Number of Gears", col=c("darkblue","red"),
legend = rownames(counts))
#带颜色和图例的堆叠条形图

计数关于你真正想要什么,有一些问题,但也许这会让你开始。我们将在这里使用R徽标的图像,以及围绕垂直线翻转的重新着色版本。首先在
pic
pic2
中设置图像。(这两个可以替换为任意两个图像。)然后创建轮廓栏并最终填充它们。有关更多想法,请参见此:

#抓取R徽标的图像

URL我想要你提供的这种形式的东西。我必须研究它,这样我就可以用它自己生成一些图表。我正在黑白打印,所以为了更清晰地查看图表,我想用不同的对象填充堆栈的各个部分,如上所述。谢谢你的指导。
# grab image of R logo
URL <- "http://www.r-project.org/Rlogo.png"
download.file(URL, "Rlogo.png", mode = "wb")
library(png)
pic <- readPNG("Rlogo.png")

# create a pink flipped version (or read in another image)
pic2 <- pic
pic2[,, 1] <- pic2[,, 4] # make pink
pic2 <- pic2[, 200:1,] # flip 

# create outline of bars
counts <- table(mtcars$vs, mtcars$gear)
bp <- barplot(counts, col = "white")

# fill in with blue and pink reversed logos
for(i in seq_along(bp)) {
  # args are image, xleft, ybottom, xright, ytop
  rasterImage(pic, bp[i]-0.5, 0, bp[i]+0.5, counts[1,i])
  rasterImage(pic2, bp[i]-0.5, counts[1,i], bp[i]+0.5, sum(counts[,i]))
}