R 动态解析离散x轴标签
根据回答的问题改编: 以下MWE中的这一行可以解析手动指定的x轴标签/值:R 动态解析离散x轴标签,r,parsing,ggplot2,R,Parsing,Ggplot2,根据回答的问题改编: 以下MWE中的这一行可以解析手动指定的x轴标签/值: scale\u x\u离散(标签=解析(text=c(“第一个值”、“第二个值”、“最后一个第三个值”)) 但是,任何尝试将c()动态替换为xo(即,包含导入表达式的有序列表)的尝试都将失败 我如何格式化或调整此专栏以使其工作?我感到困惑,因为parse命令在c()的内容上运行良好 在下面的模拟数据帧中,为了简单起见,我在这个示例中包含的唯一字符是~(解析并产生空间)。完整上下文将包含从外部管理的表导入的上标、下标、符号
scale\u x\u离散(标签=解析(text=c(“第一个值”、“第二个值”、“最后一个第三个值”))
但是,任何尝试将c()
动态替换为xo
(即,包含导入表达式的有序列表)的尝试都将失败
我如何格式化或调整此专栏以使其工作?我感到困惑,因为parse命令在c()
的内容上运行良好
在下面的模拟数据帧中,为了简单起见,我在这个示例中包含的唯一字符是~
(解析并产生空间)。完整上下文将包含从外部管理的表导入的上标、下标、符号和希腊字符
MWE:
库(ggplot2)
打印(“程序启动”)
zclass(xo)
调查让我意识到,我试图使用类型为factor的对象,我认为该对象在parse
中作为参数text
处理得不好
与其尝试删除级别和因素,它似乎更容易、更稳定地转换为字符列表(我一直认为是这样)
库(ggplot2)
打印(“程序启动”)
为什么不对x使用有序因子?然后,因子排序基于c,因子标签基于x。
library(ggplot2)
print("Program started")
z <- c("1","2","3")
x <- c("The~First~Value","A~Second~Value","Finally~Third~Value")
s <- c("No","No","No","Yes","Yes","Yes")
y <- c(1,2,3,2,3,4)
df <- as.data.frame(cbind(x=c(x,x),s=s,y=y,z=c(z,z)))
##########################################################################
xo <- as.data.frame(cbind(z,x))
xo <- xo[,"x"]
df[,"x"] <- factor(df[,"x"], levels=xo,ordered=TRUE)
##########################################################################
#xo <- levels(droplevels(xo))
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",width=.5)
#gg <- gg + scale_x_discrete(labels=parse(text=c("The~First~Value","A~Second~Value","Finally~Third~Value")))
gg <- gg + scale_x_discrete(labels=parse(text=c(xo)))
print(gg)
print("Program complete - a graph should be visible.")
library(ggplot2)
print("Program started")
z <- c("1","2","3")
x <- c("The~First~Value","A~Second~Value","Finally~Third~Value")
s <- c("No","No","No","Yes","Yes","Yes")
y <- c(1,2,3,2,3,4)
df <- as.data.frame(cbind(x=c(x,x),s=s,y=y,z=c(z,z)))
##########################################################################
xo <- as.data.frame(cbind(z,x))
xo <- xo[,"x"]
df[,"x"] <- factor(df[,"x"], levels=xo,ordered=TRUE)
##########################################################################
xo <- as.character(xo)
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",width=.5)
#gg <- gg + scale_x_discrete(labels=parse(text=c("The~First~Value","A~Second~Value","Finally~Third~Value")))
#gg <- gg + scale_x_discrete(labels=parse(text=x))
gg <- gg + scale_x_discrete(labels=parse(text=xo))
print(gg)
print("Program complete - a graph should be visible.")