使用一系列不一致的数据控制ggplot2图形中的列宽 [英] Control column widths in a ggplot2 graph with a series and inconsistent data
问题描述
在我为MWE创建的仿真数据中,我试图展示我在R中创建的脚本的本质。从该代码生成的图形可以看出,在我的一个条件中,我没有No值来完成该系列。
有人告诉我,除非我可以把最后一列遗憾地没有额外的系列一样薄,否则在图中我不会被允许使用这些图表。这是一个令人遗憾的问题,因为我编写的脚本可以同时生成数百个图表,并附有统计数据,重要性指标,传播误差条和智能y轴调整(这些功能当然不存在于MWE中)。
其他评论: 在图表的最后...所以手动调整以强制系列改变颜色并颠倒顺序,留下右侧的额外空间是不可靠的。 > MWE:
库(ggplot2)
print(程序启动)
x <-c(1,2,3,1,2,3,4)
s <-c(No, 否,否,是,是,是,是)
y < - c(1,2,3,2,3,4,5)$ b $ (df)
$ b $ gg < - ggplot(data = df, aes_string(X = ×, y =y,weight =y,ymin = paste0(y),ymax = paste0(y),fill =s));
dodge_str< - position_dodge(width = NULL,height = NULL);
gg< - gg + geom_bar(position = dodge_str,stat =identity,size = .3,color =black)
print(gg)
print(Program complete - a graph should be visible。)
如下所示,您可以自行计算条形图的x坐标,您可以得到一张可能接近您要查找的图表。
(1,2,3,1,2,3,4)
s <-c(否,否,否,是,是,是,是)
y < - c(1,2, 3,2,3,4,5)
df< - data.frame(cbind(x,s,y))
df $ x_pos [order(df $ x,df $ s)] < - 1:nrow(df)
x_stats< - as.data.frame.table(table(df $ x),responseName =x_counts)
x_stats $ center< - tapply df $ x_pos,df $ x,mean)
df< - merge(df,x_stats,by.x =x,by.y =Var1,all = TRUE)
bar_width< ; - .7
df $ pos< - apply(df,1,function(x){xpos = as.numeric(x [4])
if(x [5] == 1) xpos
else ifelse(x [2] ==No,xpos + .5 - bar_width / 2,xpos - .5 + bar_width / 2)})
print(df)
gg< ; - ggplot(data = df,aes(x = pos,y = y,fill = s))
gg < - gg + geom_bar(position =identity,stat =identity,size = 3,color =black,width = bar_width)
gg < - gg + scale_x_continuous(breaks = df $ center,labels = df $ x)
plot(gg)
-----编辑--------------------- -----------------------------
修改后放置标签
给出以下图表
In the artificial data I have created for the MWE below I have tried to demonstrate the essence of a script I have created in R. As can be seen by the graph that gets produced from this code, on one of my conditions I don't have a "No" value to complete the series.
I have been told that unless I can make this last column that sadly doesn't have the extra series as thin as the columns else where in the graph I won't be permitted to use these graphs. This is sadly a problem because the script I have written produces hundreds of graphs simultaneously, complete with stats, significance indicators, propogated error bars, and intelligent y-axis adjustments (these features are of course not present in the MWE).
Few other comments:
This exception column is not guaranteed to be at the end of the graph... so manual tweaking to force the series to change color and invert the order leaving the extra space on the right hand side isn't reliable.
I have tried to simulate the data as a constant 0 so that the series "is present" but invisible, but as would be expected, the order of the series c(No,Yes) makes this skip a space which is also unacceptable. This is how this same question was answered here, but sadly it doesn't work for me with my restrictions: Consistent width for geom_bar in the event of missing data and Include space for missing factor level used in fill aesthetics in geom_boxplot
I also tried to do this with facets but numerous issues arose there including line breaks, and errors in the annotations I add to the x-axis.
MWE:
library(ggplot2)
print("Program started")
x <- c("1","2","3","1","2","3","4")
s <- c("No","No","No","Yes","Yes","Yes","Yes")
y <- c(1,2,3,2,3,4,5)
df <- as.data.frame(cbind(x,s,y))
print(df)
gg <- ggplot(data = df, aes_string(x="x", y="y", weight="y", ymin=paste0("y"), ymax=paste0("y"), fill="s"));
dodge_str <- position_dodge(width = NULL, height = NULL);
gg <- gg + geom_bar(position=dodge_str, stat="identity", size=.3, colour = "black")
print(gg)
print("Program complete - a graph should be visible.")
解决方案 At the expense of doing your own calculation for the x coordinates of the bars as shown below, you can get a chart which may be close to what you're looking for.
x <- c("1","2","3","1","2","3","4")
s <- c("No","No","No","Yes","Yes","Yes","Yes")
y <- c(1,2,3,2,3,4,5)
df <- data.frame(cbind(x,s,y) )
df$x_pos[order(df$x, df$s)] <- 1:nrow(df)
x_stats <- as.data.frame.table(table(df$x), responseName="x_counts")
x_stats$center <- tapply(df$x_pos, df$x, mean)
df <- merge(df, x_stats, by.x="x", by.y="Var1", all=TRUE)
bar_width <- .7
df$pos <- apply(df, 1, function(x) {xpos=as.numeric(x[4])
if(x[5] == 1) xpos
else ifelse(x[2]=="No", xpos + .5 - bar_width/2, xpos - .5 + bar_width/2) } )
print(df)
gg <- ggplot(data=df, aes(x=pos, y=y, fill=s ) )
gg <- gg + geom_bar(position="identity", stat="identity", size=.3, colour="black", width=bar_width)
gg <- gg + scale_x_continuous(breaks=df$center,labels=df$x )
plot(gg)
----- edit --------------------------------------------------
Modified to place the labels at the center of bars.
Gives the following chart
这篇关于使用一系列不一致的数据控制ggplot2图形中的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!