R帮助图例中的折线图

问题描述 投票:0回答:2

因此,我目前正在使用R从excel工作表中绘制数据。我遇到的问题与图例有关。这是图片:https://i.stack.imgur.com/Key98.jpg如在图例中所见,值如下:PP1,PP10,PP15,PP3,PP30,PP5。我一直在尝试使其按数字顺序显示,例如PP1,PP3,PP5,PP10,PP15,PP30。我不确定如何解决此问题,因为我是R编码的新手。任何帮助将不胜感激!!这是我格式化Excel工作表的方式:https://i.stack.imgur.com/OfNaY.jpg这是我的代码:

library("dplyr")
install.packages("ggplot2")
library("ggplot2")
install.packages("tidyverse")
library("tidyverse")
install.packages('reshape')
library('reshape')

    # import data
    NPPdata <- read.csv("C:\\Users\\rrami\\Desktop\\R-Data\\NPPdata.csv", header = TRUE)

    ggplot(NPPdata , aes(x = N_Gradient, y=Values, colour = Group))+
          geom_errorbar(aes(ymin=Values-Stdvalue, ymax=Values+Stdvalue), lwd =1.2)+
          geom_line(lwd=1.5)+
          ggtitle("Year 1 MONO Phrag [Branch Prob 0.1]")+
          theme(plot.title = element_text(hjust =0.5)) +
          labs(x = "N-Gradient", y ="INV%")+
           theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
            axis.text.y = element_text(size = 14), axis.title.y = element_text(size = 16))
r ggplot2
2个回答
0
投票

NPPdata中的顺序如何?您可以更改数据框中的顺序。


0
投票

我已经用“ iris”作为例子。如您所见,在第二个数字上,“ scale_fill_discrete”用于更改标签的顺序。

library (tidyverse)

data(iris)

figure_1 <- iris %>% 
  gather(key = floral_components, value = values, -Species) %>% 
  ggplot(aes(x = floral_components, y = values, fill = Species)) +
  geom_bar(stat='identity') +
  labs(x = "Floral Components",
        y = "Values", 
        fill = "Species")

enter image description here

figure_2 <- iris %>% 
  gather(key = floral_components, value = values, -Species) %>% 
  ggplot(aes(x = floral_components, y = values, fill = Species)) +
  geom_bar(stat='identity') +
  labs(x = "Floral Components",
       y = "Values", 
       fill = "Species") +
  scale_fill_discrete(labels = c("versicolor", "virginica", "setosa")) 

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.