No legend in ggplot line graph

我的未来我决定 提交于 2019-12-13 11:03:57

问题


I am trying to generate a legend for a line graph I have created using ggplot. From what I have been reading, the legend should be made automatically given I have changed the aesthetics of the graph and included the linetype within the aes function but it is not visible. I have been scouring through answers to this exact question online and have tried many approaches but nothing seems to be working. I think I am missing something quite minor. I tried using scale_linetype_manual but this did not produce a legend. Any help would be appreciated.

    jpeg(filename = "careercurve.jpeg", width = 12, height = 10, units = "cm", res = 1200)
  ggplot() +
    scale_y_continuous(limits = c(0.5,0.8))+
   geom_line(aes(x = Age, y = fwd.preds), data = fwd.predictions, linetype = 1) +
    geom_line(aes(x = Age, y = def.preds), data = def.predictions, linetype = 2) +
    geom_line(aes(x = Age, y = mid.preds), data = mid.predictions, linetype = 3) +
    geom_line(aes(x = Age, y = ruck.preds), data = ruck.predictions, linetype = 4) +
    theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.text = element_text(colour = "black", size = 8), axis.title = element_text(size = 8), legend.position = "bottom")+
    labs(x = "Age (years)", y = "AFL Player Rank/min (au)") +
    geom_text(aes(x = 35.4, y = 0.535, label = "Fwd"), size = 3) +
    geom_text(aes(x = 35.4, y = 0.50, label = "Def"), size = 3) +
    geom_text(aes(x = 35.4, y = 0.575, label = "Mid"), size = 3) +
    geom_text(aes(x = 35.4, y = 0.67, label = "Ruck"), size = 3)
  dev.off()

回答1:


The easiest way to work with ggplot2 is if you can get all your data in a single dataframe, with columns to identify any important groups. So in your case, you need to give all the fwd.preds columns in your separate dataframes a common name, and add a new column to identify the group. Then you can combine them and plot, e.g.:

library(dplyr)

# Separate dfs
fwd_df = data.frame(
    Age = rpois(10, 20),
    fwd.preds = rnorm(10)
)
def_df = data.frame(
    Age = rpois(10, 20),
    def.preds = rnorm(10)
)

# Make all the column names the same, add
#   a position column
fwd_df = fwd_df %>%
    mutate(Position = "fwd") %>%
    rename(Pred = fwd.preds)
def_df = def_df %>%
    mutate(Position = "def") %>%
    rename(Pred = def.preds)
# Combine into single df
combined_df = bind_rows(fwd_df, def_df)

ggplot(combined_df, aes(x = Age, y = Pred, linetype = Position)) +
    geom_line()

Note that we can now just include the Position column in aes(), and ggplot handles the legend and different linetypes automatically.




回答2:


This is because your linetype is not within the 'aes' brackets but is rather a series of constants. I don't know what your datasets look like, but the simplest solution would probably be to create a column in each dataframe that is the name of the data set.

Then you would have

geom_line(aes(x = Age, y = ..., linetype = df), data = ...) 

However, a better solution, I suspect, would involve combining the data into the one dataframe, even if it's only in a temporary data frame.



来源:https://stackoverflow.com/questions/52960885/no-legend-in-ggplot-line-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!