Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更改GGR绘图中的字母顺序_R_Ggplot2_Line_Alphabetical Sort - Fatal编程技术网

更改GGR绘图中的字母顺序

更改GGR绘图中的字母顺序,r,ggplot2,line,alphabetical-sort,R,Ggplot2,Line,Alphabetical Sort,我试图按照我在使用的数据帧中给它的顺序创建一个折线图,但是我每次都尝试过,当我添加诸如reorder或stat=“identity”之类的内容时,它会显示错误,那么,有人能帮我吗 #Create first data frame w/ women's information df <- data.frame(Ocupacion=c("Estudiantes", "Licenciados/as", &qu

我试图按照我在使用的数据帧中给它的顺序创建一个折线图,但是我每次都尝试过,当我添加诸如reorder或stat=“identity”之类的内容时,它会显示错误,那么,有人能帮我吗

#Create first data frame w/ women's information
df <- data.frame(Ocupacion=c("Estudiantes", "Licenciados/as",
                        "Predoctorales", "Doctores/as", "Profesores/as", 
                        "Catedraticos/as"),
                 Porcentaje=c(54.3, 60.2, 51, 49, 36.1, 14.1),
                 Gender=(c("% Mujeres")))


head(df)


#Create second data frame w/ men's information
df2 <- data.frame(Ocupacion=rep(c("Estudiantes", "Licenciados/as",
                             "Predoctorales", "Doctores/as", "Profesores/as", 
                             "Catedraticos/as"),1),
                  Porcentaje=c(45.3, 39.2, 50.7, 51, 61.2, 82),
                  Gender=(c("% Hombres")))


head(df2)

#Bind both data frames into one to create the graph
df3 <- rbind(df, df2)



# Change line types by groups (gender)

library(ggplot2)
ggplot(df3, aes(x=Ocupacion, y=Porcentaje, group=Gender)) +
  geom_line(stat = "identity", aes(linetype=Gender))+
  theme_bw() +
  ggtitle("Figura 2: Porcentaje de las mujeres y hombres en las universidades
          públicas españolas 2005-2007") +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_point()
#创建第一个包含女性信息的数据框
df您需要将“性别”作为一个因素,并输入要用作级别的顺序

#Create first data frame w/ women's information
df <- data.frame(Ocupacion=c("Estudiantes", "Licenciados/as",
                             "Predoctorales", "Doctores/as", "Profesores/as", 
                             "Catedraticos/as"),
                 Porcentaje=c(54.3, 60.2, 51, 49, 36.1, 14.1),
                 Gender=(c("% Mujeres")))


head(df)


#Create second data frame w/ men's information
df2 <- data.frame(Ocupacion=rep(c("Estudiantes", "Licenciados/as",
                                  "Predoctorales", "Doctores/as", "Profesores/as", 
                                  "Catedraticos/as"),1),
                  Porcentaje=c(45.3, 39.2, 50.7, 51, 61.2, 82),
                  Gender=(c("% Hombres")))


head(df2)

#Bind both data frames into one to create the graph
df3 <- rbind(df, df2) 

df3$Gender <-factor(df3$Gender, levels = c("% Mujeres",
                                           "% Hombres"))



# Change line types by groups (gender)

library(ggplot2)
ggplot(df3, aes(x=Ocupacion, y=Porcentaje, group=Gender)) +
  geom_line(stat = "identity", aes(linetype=Gender))+
  theme_bw() +
  ggtitle("Figura 2: Porcentaje de las mujeres y hombres en las universidades
          públicas españolas 2005-2007") +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_point()
#创建第一个包含女性信息的数据框

df
df3$Gender需要更改的是
Gender
,而不是
Ocupacion