Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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
R 手动将颜色添加到Shining/ggplot_R_Ggplot2_Shiny - Fatal编程技术网

R 手动将颜色添加到Shining/ggplot

R 手动将颜色添加到Shining/ggplot,r,ggplot2,shiny,R,Ggplot2,Shiny,我正在尝试创建一个闪亮的应用程序,用户可以在其中选择三列中的哪一列随着时间的推移进行绘制,包括三个候选列的百分比。到目前为止,实际的情节是完美的,但我想添加一些颜色,使Cand_1得到一条蓝线,Cand_2得到一条绿线,Cand_3得到一条红线。我尝试在变量名周围使用Plot+scale\u color\u manuall=“c”(“Cand\u 1”=“蓝色”、“Cand\u 2”=“绿色”、“Cand\u 3”=“红色”)加上和不加上“”,在aes()中也使用if,以便: Plot <

我正在尝试创建一个闪亮的应用程序,用户可以在其中选择三列中的哪一列随着时间的推移进行绘制,包括三个候选列的百分比。到目前为止,实际的情节是完美的,但我想添加一些颜色,使Cand_1得到一条蓝线,Cand_2得到一条绿线,Cand_3得到一条红线。我尝试在变量名周围使用
Plot+scale\u color\u manuall=“c”(“Cand\u 1”=“蓝色”、“Cand\u 2”=“绿色”、“Cand\u 3”=“红色”)
加上和不加上“”,在
aes()
中也使用
if
,以便:

Plot <- Plot + geom_line(aes(month, !! sym(input$cand)),  group = 1, if(input$cand == "Cand_1){
colour = "blue"}
if(input$cand == "Cand_2"){colour = "green"}
if(input$cand == "Cand_2"){colour = "red})
下面是一些示例数据:

Month   Cand_1  Cand_2  Cand_3
2019-02-01  60,7    90,1    86,2
2019-03-01  58,9    90,2    80,3
2019-04-01  47,3    88,3    84,6
2019-05-01  54,5    87,3    90
2019-06-01  50,6    86      89
2019-07-01  49,8    84,2    87,1

你不能这样分配颜色

Plot <- Plot + geom_line(aes(month, !! sym(input$cand)),  group = 1, if(input$cand == "Cand_1){
colour = "blue"}
if(input$cand == "Cand_2"){colour = "green"}
if(input$cand == "Cand_2"){colour = "red})
还有一些较短的方法:

color.list <- list(Cand_1 = "blue", Cand_2 = "green", Cand_3 = "red")

Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1),
  colour = color.list[[input$cand]])
color.list
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)),  group = 1, colour = <your decision here>)
Plot <- Plot + geom_line(aes(month, !! sym(input$cand)),  group = 1),
  colour = if(input$cand == "Cand_1") "blue" else 
if(input$cand == "Cand_2")"green" else
if(input$cand == "Cand_3") "red")
color.list <- list(Cand_1 = "blue", Cand_2 = "green", Cand_3 = "red")

Plot <- Plot + geom_line(aes(month, !! sym(input$cand)), group = 1),
  colour = color.list[[input$cand]])