text(0.5, 0.8, paste("Hopkins statistic = ", round(hopkins_stat, 2))) Error in text.default(0.5, 0.8, paste("Hopkins statistic = ", round(hopkins_stat, : plot.new has not been called yet
时间: 2024-01-16 21:05:29 浏览: 22
这个错误通常在你试图在没有创建图形的情况下调用 "text" 函数时出现。在调用 "text" 函数之前,你需要先使用 "plot" 函数创建一个图形。例如:
```
plot(1:10)
text(5, 5, "Hello World!")
```
这将在一个新的设备上创建一个简单的图形,并在坐标(5, 5)处添加 "Hello World!" 的文本。确保在调用 "text" 函数之前使用 "plot" 函数创建一个图形。
相关问题
p_boxplot_exp_mt.gene = boxplot(sample_exp_mt.gene) > p_boxplot_exp_mt.gene = p_boxplot_exp_mt.gene + + geom_boxplot(color = "black", size = 0.25, outlier.shape = NA) + + xlab(NULL) + ylab("log2(CPM+1)") + + scale_color_manual(values = "black") + + stat_summary(fun = max, geom = "crossbar", color = "black", width = 0.1) + + stat_summary(fun = min, geom = "crossbar", color = "black", width = 0.1) + + theme(axis.line = element_line(color = "black", size = 0.25), + axis.ticks = element_line(color = "black", size = 0.25), + axis.text = element_text(color = "black", angle = 90, size = 18), + axis.title = element_text(color = "black", size = 18)) + + geom_text(data = sample_exp_mt.gene, aes(x = sample, y = median_expression, + label = paste0("Median: ", round(median_expression, 2))), + vjust = -1, hjust = -0.5, size = 4, color = "black")+ + theme(panel.background = element_blank()) Error in p_boxplot_exp_mt.gene + geom_boxplot(color = "black", size = 0.25, : non-numeric argument to binary operator
看起来您的代码中有一些问题,主要是在 `p_boxplot_exp_mt.gene = boxplot(sample_exp_mt.gene)` 这行代码中,您将 `boxplot` 的输出结果赋值给了 `p_boxplot_exp_mt.gene` 变量。但是,在后面的代码中,您却又将 `p_boxplot_exp_mt.gene` 作为一个 `ggplot` 对象进行操作,这就导致了一个类型不匹配的错误。
建议您将 `boxplot` 的输出结果转换为一个数据框,再使用 `ggplot` 进行绘图,如下所示:
```
# 将 boxplot 的输出结果转换为数据框
df_boxplot <- as.data.frame(p_boxplot_exp_mt.gene)
# 使用 ggplot 绘制箱线图
ggplot(df_boxplot, aes(x = "", y = sample_exp_mt.gene)) +
geom_boxplot(color = "black", size = 0.25, outlier.shape = NA) +
xlab(NULL) +
ylab("log2(CPM+1)") +
scale_color_manual(values = "black") +
stat_summary(fun = max, geom = "crossbar", color = "black", width = 0.1) +
stat_summary(fun = min, geom = "crossbar", color = "black", width = 0.1) +
theme(axis.line = element_line(color = "black", size = 0.25),
axis.ticks = element_line(color = "black", size = 0.25),
axis.text = element_text(color = "black", angle = 90, size = 18),
axis.title = element_text(color = "black", size = 18)) +
geom_text(data = df_boxplot, aes(x = 1, y = median,
label = paste0("Median: ", round(median, 2))),
vjust = -1, hjust = -0.5, size = 4, color = "black") +
theme(panel.background = element_blank())
```
这样可以避免 `p_boxplot_exp_mt.gene` 类型不匹配的错误。
a=get_clust_tendency(scale(state.x77), nrow(state.x77)-1, graph = TRUE) #scale:对数据集进行标准化 a$hopkins_stat plot(a$plot)。绘制的图形里没有hopkins的值啊
您可以尝试使用以下代码来获取Hopkins统计量的值:
```
library(factoextra)
library(cluster)
data <- scale(state.x77)
hopkins_stat <- get_clust_tendency(data, nrow(data) - 1, graph = FALSE)$hopkins_stat
print(hopkins_stat)
```
这将输出Hopkins统计量的值。如果您想要在绘图中显示Hopkins统计量的值,可以使用以下代码:
```
library(factoextra)
library(cluster)
data <- scale(state.x77)
hopkins_stat <- get_clust_tendency(data, nrow(data) - 1, graph = TRUE)$hopkins_stat
plot(get_clust_tendency(data, nrow(data) - 1, graph = TRUE)$plot)
text(0.5, 0.8, paste("Hopkins statistic = ", round(hopkins_stat, 2)))
```
这将在绘图中显示Hopkins统计量的值。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)