我对R没有多少经验,因为我想在R中创建一个相当具体的图表,我希望你们中的一些人可以帮助我。
我有五个不同数据集上使用的四个分类器的结果数据。为了获得准确的结果,每个分类器在同一数据集上运行10次。所以现在我有一个结果表如下:
string input = "3 days ago";
string sFirstCharacter = input.Substring(0, 1);
int nFirstCharacter = int.Parse(input.Substring(0, 1));
等等。 我想要为我的图形获得的是有四个不同颜色的单独图形代表四个分类器。 y轴只表示分类的结果,x轴应该描绘五个不同的数据集。 每个"标记"在x轴上应该是一个数据集,每个图表在y轴上的点将是该特定数据集上该分类器的10个结果的平均值。
我尝试使用ggplot2通过从数据中创建数据框并将数据集名称作为变量进行融合来实现此目的。我可能不会真正理解融化的真正含义。
如果我的描述笨拙且缺乏,我对创建图表和情节并不是很熟悉并道歉。 我非常感谢你的帮助。
答案 0 :(得分:0)
TL; DR:使用facets重新格式化输入数据+ ggplot
由于未提供测试数据,我创建了虚拟数据
library(dplyr)
library(tidyr)
library(ggplot2)
set.seed(1)
dummydata <-
matrix(
data = sample(do.call("c", select(iris, -Species)), 10*5*4, replace = T),
nrow=4, ncol=10*5
)
rownames(dummydata) <- paste0("Classifier", 1:4)
colnames(dummydata) <- rep(paste0("DataSet", 1:5), each=10)
dummydata
这里,dummydata是一个看起来像
的矩阵# DataSet1 DataSet1 ... (x10 total columns of each Dataset) ... DataSet5
# Classifier1 3.1 5.6 ...
# Classifier2 2.8 1.3 ...
# ...
# Classifier4 1.3 ...
我们这样做:
## Make col names unique
colnames(dummydata) <- paste(colnames(dummydata), 1:10, sep="_")
dummydata_reformat <-
dummydata %>%
## make sure it is data frame
## with a column for the classifier type i.e. rowname
as.data.frame() %>%
tibble::rownames_to_column("classifier") %>%
## reformat data
gather(dataset,value,-classifier) %>%
separate(dataset, into=c("dset", "x"), sep="_")
数据现在看起来像
#> dummydata_reformat
# classifier dset x value
#1 Classifier1 DataSet1 1 3.1
#2 Classifier2 DataSet1 1 2.8
#3 Classifier3 DataSet1 1 1.6
# ...
## Plot
dummydata_reformat %>%
ggplot(aes(x=dset,
y=value
## can add: "color = classifier" to color by classifier
## but since you are splitting the plots by classifier,
## this does not make sense
)) +
geom_point() +
xlab("") +
facet_wrap(~classifier) +
theme(
## rotate the x-axis to fit text
axis.text.x = element_text(angle=90, hjust=1, vjust=0.5)
)