unable to get legends in plot with multiple line

i am not able to get the legend in the ggplot. I am sharing the codes and sample data.

Thanks in advance

ggplot(data_plot, aes(x = year, y = cr5))+
  geom_line(lty=1,lwd=1)+
  geom_line(aes(y = cr10), lty = 4,lwd=1) +
  geom_line(aes(y = hhi), lty = 5,lwd=1) +
  ylim(0, 1)+
  labs(title = "Concentration and Herfindahl-Hirschman Index (HHI) during 1998-2022.",
       x = "", y = "") +
  legend("right", legend = c("CR5","CR10","HHI"), lty = c(1,3,5))

data

year	cr5	cr10	hhi
1998	0.442865985	0.602359898	0.068173993
1999	0.442112595	0.59911706	0.070629232
2000	0.428284123	0.58547981	0.069995583
2001	0.427560433	0.57782029	0.073030846
2002	0.420737851	0.589867065	0.069163224
2003	0.41490982	0.579500901	0.066778344
2004	0.405780603	0.562992614	0.062079985
2005	0.409185991	0.56774689	0.061313117
2006	0.409889632	0.567925388	0.057598804
2007	0.400818962	0.555345738	0.05447507
2008	0.391401773	0.552378214	0.054399859
2009	0.390753752	0.559887514	0.057698474
2010	0.378917568	0.562713007	0.054927221
2011	0.378429764	0.565822881	0.053595005
2012	0.374710642	0.561201979	0.050980428
2013	0.374360531	0.5612071	0.051485629
2014	0.380564753	0.567547842	0.05196086
2015	0.386667899	0.581758511	0.054680071
2016	0.392207847	0.581341906	0.056336758
2017	0.424217031	0.618156689	0.065374265
2018	0.451438767	0.632555218	0.075765853
2019	0.458840157	0.64161298	0.076725092
2020	0.518466992	0.706075594	0.087544266
2021	0.512412498	0.7482065	0.089257398
2022	0.516847614	0.750248724	0.089732408

The easiest way to do this is to pivot the data so a single column stores the values to be plotted and another column labels values as cr5, cr10, or hhi. ggplot() builds a legend from the mapping of values in a column to an aesthetic such as linetype.

library(ggplot2)
library(tidyr)
data_plot <- read.csv("~/R/Play/Dummy.csv")
data_plot_long <- pivot_longer(data_plot,cols = -year,
                               names_to = "Type", values_to = "Value")
head(data_plot_long)
#> # A tibble: 6 × 3
#>    year Type   Value
#>   <int> <chr>  <dbl>
#> 1  1998 cr5   0.443 
#> 2  1998 cr10  0.602 
#> 3  1998 hhi   0.0682
#> 4  1999 cr5   0.442 
#> 5  1999 cr10  0.599 
#> 6  1999 hhi   0.0706

ggplot(data_plot_long, aes(x = year, y = Value, linetype = Type))+
  geom_line(lwd=1)+
  scale_linetype_manual(values = c(cr5 = 1, cr10 = 3, hhi = 5))+
  ylim(0, 1)+
  labs(title = "Concentration and Herfindahl-Hirschman Index (HHI) during 1998-2022.",
       x = "", y = "")

Created on 2023-07-29 with reprex v2.0.2

1 Like

Thanks a lot in advance.

How to position the lenends below the graph parallel to x axis.

Thanks in advance.

ggplot(data_plot_long, aes(x = year, y = Value, linetype = Type))+
  geom_line(lwd=1)+
  scale_linetype_manual(values = c(cr5 = 1, cr10 = 3, hhi = 5))+
  ylim(0, 1)+
  labs(title = "Concentration and Herfindahl-Hirschman Index (HHI) during 1998-2022.",
       x = "", y = "") +
  theme(legend.position = "bottom")`
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.