因此,我目前正在使用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))
NPPdata中的顺序如何?您可以更改数据框中的顺序。
我已经用“ 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")
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"))