简体   繁体   English

R 中 scale_color_discrete() 函数的图例问题

[英]Problem with the legend with scale_color_discrete() function in R

I've this data我有这个数据

tempi_k_random <- c(2.849072363496035E-6,4.884230846336759E-6,7.420560612989667E-6,9.320163959989317E-6,1.125275640739933E-5,1.3818609943264999E-5,1.5799511999414483E-5,1.79927469933985E-5,1.958275275649878E-5,2.1785199360099794E-5,2.4198109784553963E-5,4.54269327069883E-5,6.518881674784684E-5,8.52658233138289E-5,1.0445134492757438E-4,1.268194860165957E-4,1.4795886846906084E-4,1.7116724615770838E-4,1.9454743217185282E-4,2.095390879427764E-4,4.1366770908908134E-4,6.202640042883945E-4,8.379609645905531E-4,0.0010454638386975343,0.001188950746965453,0.0014212418300653593,0.0017239767156862745,0.0018250641025641023,0.002074198717948718,0.0040149999999999995,0.0059812500000000005,0.008516666666666665,0.011816666666666666,0.0189875,0.020087499999999998,0.0222125,0.016399999999999998,0.019649999999999997,0.0282,0.04525,0.0523,0.06235,0.0798,0.09570000000000001,0.10475,0.12225,0.1352)

tempi_k_cinque <- c(2.185171516292494E-6,3.688659557278267E-6,5.086848744974997E-6,6.550152577270055E-6,8.190041618674074E-6,9.569229015801014E-6,1.130849767469026E-5,1.2768555980732062E-5,1.4900814742468114E-5,1.5916628540808872E-5,1.7213466225813496E-5,3.214082728715961E-5,4.5726078618563776E-5,6.314850708691957E-5,8.436387207939846E-5,9.751538176472509E-5,1.0571751299724081E-4,1.1758304250994831E-4,1.3748931047676842E-4,1.480619766765244E-4,3.119003987090575E-4,4.3609348952131457E-4,6.194213313568152E-4,7.484261873392308E-4,8.79766911150425E-4,0.0010233251633986926,0.001126502976190476,0.0013468323343323345,0.001551022727272727,0.002741190476190476,0.004115000000000001,0.0052,0.0084125,0.0105125,0.01695,0.019,0.013525,0.016325,0.02135,0.03055,0.038,0.045149999999999996,0.05875,0.07305,0.0888,0.1039,0.09995)

tempi_ndieci <- c(2.375526248273769E-6,4.256132067044228E-6,6.042428977111871E-6,8.615698869627676E-6,9.95753073736447E-6,1.1776845215643598E-5,1.3660545425457787E-5,1.5924493501608036E-5,1.7668195352664178E-5,1.9221459566688867E-5,2.045119723829764E-5,3.811749152074419E-5,6.038099988218772E-5,7.483761391315114E-5,9.55648261718686E-5,1.1421521459312587E-4,1.353618000970425E-4,1.593224149232047E-4,1.664030090266135E-4,1.8585104947233018E-4,3.6222878593326967E-4,5.813731241132015E-4,7.396324821243827E-4,8.994984672166082E-4,0.0011234896715603237,0.001304954374293577,0.0013452083333333332,0.0016280505952380952,0.0018492628205128204,0.0037657738095238096,0.00477625,0.007389583333333334,0.010470833333333334,0.0153375,0.0194375,0.0166,0.0146,0.01645,0.022949999999999998,0.038,0.0464,0.05675,0.0742,0.08495,0.09765,0.10375,0.11015000000000001)

tempi_nmenoc <- c(2.28491345365468E-6,3.529909336343534E-6,5.025295275675399E-6,6.514100794323373E-6,8.058699140564791E-6,9.471434112359757E-6,1.0944375376030776E-5,1.2384409686795722E-5,1.4036495365797998E-5,1.5624169208365355E-5,1.6777502364941904E-5,3.14744111238494E-5,4.5192996614103015E-5,6.243421855589104E-5,7.603017835619405E-5,8.73392281204343E-5,1.0611253966143871E-4,1.2097295470045547E-4,1.3288902062298435E-4,1.4510677837849405E-4,2.898812731152251E-4,4.287646414347314E-4,5.860436388208565E-4,7.231695424141076E-4,8.790873015873016E-4,0.0010133180684554523,0.0010897767857142857,0.001376007326007326,0.0014679807692307692,0.002784523809523809,0.004365,0.004866666666666666,0.008370833333333334,0.0112625,0.0145375,0.016237500000000002,0.014225,0.016300000000000002,0.0238,0.032049999999999995,0.0437,0.04775,0.0581,0.0665,0.08595,0.0955,0.10055)


#Input da 100 a 10'000'100
lunghezzaInput <- c(100,200,300,400,500,600,700,800,900,1000,1100,2100,3100,4100,5100,6100,7100,8100,9100,10100,20100,30100,40100,50100,60100,70100,80100,90100,100100,200100,300100,400100,500100,600100,700100,800100,900100,1000100,2000100,3000100,4000100,5000100,6000100,7000100,8000100,9000100,10000100)

I've create this data frame我已经创建了这个数据框

df <- data.frame(lunghezzaInput,tempi_k_random,tempi_k_cinque,tempi_ndieci,tempi_nmenoc)

and with ggplot2 library I've made this representation :和 ggplot2 库我做了这个表示:

options(scipen=999)

gg <- ggplot(df,aes(x=lunghezzaInput, y=tempi_k_random, colour = "blue")) +
  scale_color_discrete("Tempi") +
  geom_smooth(method="loess", se=F) + 
  geom_smooth(aes(y=tempi_k_cinque,colour = "red"), method = "loess", se=F) +
  geom_smooth(aes(y=tempi_ndieci, colour = "#3DE61A"), method = "loess", se=F) +
  geom_smooth(aes(y=tempi_nmenoc, colour = "#FFD13B"), method = "loess", se=F) +
  labs(subtitle="Analisi dei tempi di Median of Medians", 
       y="Tempo (s)", 
       x="Grandezza Input", 
       title="Median of Medians")
  

plot(gg)

But in this image I want to change the legend element's name.但在这张图片中,我想更改图例元素的名称。

在此处输入图片说明

How can I call them我怎么称呼他们

"K = Random"
"K = n - 5"
"k = n/10"
"k = 5" ??

This type of problems generaly has to do with reshaping the data.这类问题通常与重塑数据有关。 The format should be the long format and the data is in wide format.格式应该是长格式,数据是宽格式。 See this post on how to reshape the data from wide to long format.请参阅这篇关于如何将数据从宽格式重塑为长格式的帖子

First, create a named vector of color values.首先,创建一个命名的颜色值向量
Then reshape the data, pipe directly to ggplot and then use only one geom_smooth .然后重塑数据,直接管道到ggplot然后只使用一个geom_smooth

library(dplyr)
library(tidyr)
library(ggplot2)

tempi_names <- c("K = random", "K = n - 5", "k = n/10", "k = 5")
gg_colors <- c('#0000FF', '#FF0000', '#3DE61A', '#FFD13B')
gg_colors <- setNames(gg_colors, names(df)[-1])

df %>%
  pivot_longer(
    cols = starts_with('tempi'),
    names_to = 'Tempi',
    values_to = 'Value'
  ) %>% 
  ggplot(aes(x = lunghezzaInput, y = Value, colour = Tempi)) +
  geom_smooth(formula = y ~ x, method = 'loess', se = FALSE) +
  scale_colour_manual(name = "Tempi", 
                      breaks = names(df)[-1],
                      labels = tempi_names,
                      values = gg_colors) +
  labs(subtitle="Analisi dei tempi di Median of Medians", 
       y="Tempo (s)", 
       x="Grandezza Input", 
       title="Median of Medians")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM