R ggplot:如何使用列名作为x轴标签,使用值作为y轴标签
找不到解决这个问题的方法 我想绘制一个包含4列的数据框,如下所示: 种 今天的意义 平均RCP26 平均RCP85 1. 0.567 0.765 0.342 2. 0.987 0.543 0.001 3. 0.456 0.876 0.54R ggplot:如何使用列名作为x轴标签,使用值作为y轴标签,r,ggplot2,R,Ggplot2,找不到解决这个问题的方法 我想绘制一个包含4列的数据框,如下所示: 种 今天的意义 平均RCP26 平均RCP85 1. 0.567 0.765 0.342 2. 0.987 0.543 0.001 3. 0.456 0.876 0.54 您需要首先旋转数据,这是通过聚集实现的,您还需要组美学来获得具有离散x轴的线 require(dplyr) require(ggplot2) df %>% gather(var, val , -Species) %>% ggplot(
您需要首先旋转数据,这是通过
聚集
实现的,您还需要组
美学来获得具有离散x轴的线
require(dplyr)
require(ggplot2)
df %>%
gather(var, val , -Species) %>%
ggplot(aes(x = var, y = val, color = as.factor(Species), group = as.factor(Species)))+
geom_line()
我还使用了
tidyr
来透视表
library(ggplot2)
library(dplyr)
library(tidyr)
df %>%
pivot_longer(cols = 2:4, names_to = "indicators", values_to = "values") %>%
ggplot(data = ., aes(x = indicators, y = values, color = as.factor(Species), group = Species)) +
geom_point()
输出:
请以便于受访者使用的方式显示您的数据。gather()的开发已经完成,对于新代码,我们建议切换到pivot_longer()。