在R的调色板中保存颜色

问题描述

我想保存自动ggplot功能分配给绘图中每个工作站的颜色。我想将分配给每个工作站的颜色保存在调色板中,以便在其他绘图中再次使用:

ggplot(DSF_moments,aes(x=year,y=max,group = station,colour = station)) + 
  geom_line(size = 1)  + 
  geom_point(size=1.5,shape=21,fill="white") + 
  labs(y ="Annual max flow [m3/s]",x = "year",title = "Annual Maximum Streamflow",size = 50) +
  theme(plot.title = element_text(size=16),axis.text.y = element_text(size=11),axis.text.x = element_text(angle = 90,size=11)) + scale_x_continuous (breaks=seq(min(DSF_moments$year),max(DSF_moments$year),by=2)) +
  scale_y_continuous (breaks=seq(min(DSF_moments$max),max(DSF_moments$max),by=5000))
dev.copy(png,"Plot_Max_Annual_RawData.png",width=22,height=11,units="in",res=100)
dev.off()

使用上面代码中的颜色功能,ggplot为每个工作站分配一种颜色,我不想更改颜色,我只想知道为每个工作站分配了哪种颜色。想法是为每个工作站分别在绘图后生成,但保持先前在所有工作站中在第一个公共绘图中分配的颜色。

for (i in 1:length(listDF2)) 
{
  df1 <- as.data.frame(listDF2[[i]])
  df1[is.na(df1)] <- 0
  temp_plot <- ggplot(df1,aes(x = day,y = DailyMeanStreamflow,colour=Station[i])) +
  geom_line(size = 1)  + 
  geom_point(size=1.5,fill="white") + 
  facet_wrap(~ month,ncol = 3) +
  labs(title = "Daily Mean Streamflow",subtitle = "Data plotted by month",y = "Daily Mean Streamflow [m3/s]",x="Days") + 
  scale_x_continuous (breaks=seq(1,max(df1$day),by=1)) + theme(axis.text.x = element_text(size=9))

  print(temp_plot)

  name4<- paste("DailyStreamflow_byMonth","_",siteNumber[i],".png",sep="")
  ggsave(temp_plot,filename = name4,dpi=500)
  dev.off()
}

我现在要为每个图形分配以前分配的颜色。如何通过ggplot将分配的认颜色保存到每个工作站?

电台的格式为:“ 094985005”,“ 09498501”,“ 09489500”

解决方法

answers linked in the comments有很多有用的信息,我在此显示的是基于这些信息。

# importing library
import kivy

# version
kivy.require('1.11.1')

# importing functionality
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager,Screen

# defining screens
class LoginWindow(Screen):
    pass
class PreferencesWindow(Screen):
    pass
class HomeWindow(Screen):
    pass
class WindowManager(ScreenManager):
    pass

# background
Window.clearcolor = (0.67,0.83,0.88,1)

# creating layout class
class MyFloatLayout(FloatLayout):
    username = ObjectProperty(None)
    password = ObjectProperty(None)

    # defining processing method of the login button
    def check_login(self):

        """
        Processing,will fix later
        """

        print("Login succesful!")

        # reset the textinputs to empty strings once pressed and processed
        self.username.text = ''
        self.password.text = ''

        # navigate to home screen
        app.root.current = "home"
        app.root.transition.direction = "right"

# linking .py with .kv
kv = Builder.load_file('gui.kv')

# creating application class that returns variable kv
class MyApp(App):
    def build(self):
        return (kv)

if __name__ == '__main__':
    app = MyApp()
    app.run()

由于您尚未共享任何数据,因此未经测试,但这至少可以使您非常接近。如果您需要更多帮助,请分享一个可复制的示例。

,

另一种方法是使所有数据帧的因子水平保持相同,请参见示例:

# example data
listdf <- list(
  data.frame(x = 1:1,y = 1:2,station = c("094985005","09498501")),data.frame(x = 1:1,y = 2:3,station = c("09498501","09489500"))
)

#fix levels
allStations <- sort(unique(unlist(lapply(listdf,"[[","station"))))
listdf[[1]]$station <- factor(listdf[[1]]$station,levels = allStations)
listdf[[2]]$station <- factor(listdf[[2]]$station,levels = allStations)

#plot side by side to illustrate the same levels
cowplot::plot_grid(
  ggplot(listdf[[1]],aes(x,y,col = station)) +
    geom_point(size = 5) +
    scale_color_discrete(drop = FALSE) +
    ylim(0,3),ggplot(listdf[[2]],3)
)

enter image description here