在ggplot2中返回输入值

时间:2017-09-24 15:36:28

标签: r ggplot2

鉴于此代码:

Example.Data<- data.frame(matrix(vector(), 0, 3, dimnames=list(c(), c("Value", "Variable", "Fill"))), stringsAsFactors=F)

Example.Data[1, ] <- c(45, 'Horizontal Pattern','Horizontal Pattern' )
Example.Data[2, ] <- c(65, 'Vertical Pattern','Vertical Pattern' )
Example.Data[3, ] <- c(89, 'Mesh Pattern','Mesh Pattern' )
Example.Data[4,] <- c(20, 'Diagonal Pattern','Diagonal Pattern' )

HighlightDataVert<-Example.Data[2, ]
HighlightHorizontal<-Example.Data[1, ]
HighlightMesh<-Example.Data[3, ]
HighlightHorizontal$Value<-as.numeric(HighlightHorizontal$Value)
Example.Data$Value<-as.numeric(Example.Data$Value)


Example.Data$Variable = Relevel(Example.Data$Variable, ref = c("Horizontal Pattern","Vertical Pattern","Mesh Pattern","Diagonal Pattern"))

example_plot<-ggplot(Example.Data, aes(x=Variable, y=Value, fill=Fill)) + theme_bw() + #facet_wrap(~Product, nrow=1)+ #Ensure theme_bw are there to create borders
  theme(legend.position = "none")+
  scale_fill_grey(start=.4)+
  #scale_y_continuous(limits = c(0, 100), breaks = (seq(0,100,by = 10)))+
  geom_bar(position=position_dodge(.9), stat="identity", colour="black", legend = FALSE, width=0.333333333)

如何在上面的width中返回geom_bar(0.3333333)的输入值?

我可以按如下方式打印position

contents_struct_of_layers = capture.output(str(example_plot$layers))
#splits width lines into list
split_string<-as.list(strsplit(contents_struct_of_layers[39], " ")[[1]])
input_position_dodge<-as.numeric(split_string[10])
input_position_dodge

但在审核example.plot的内容后,我无法找到geom_bar position的相应值。

您可以使用以下代码查看example.plot及其各种组件的结构:

str(example_plot$layers)

STR(example_plot $主题)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以在example_plot$layers[[1]]$geom_params$width

中找到宽度