2017-04-20 149 views
1

我是R的新手,我沒有得到任何線索,先說明一下。如何在R中加入兩條彩色陰謀線加上兩條y軸

我保存到data與插頭Cycle,Instances,MaxFitness,MinFitness 其中循環只是數字的線,所述實例去超過0和100與適應值之間的竇是所期望的0.5和5

之間csv文件輸出是一個單一的svg或pdf文件,其中我有在x軸上的週期,在左側y軸上的適應性和在右側y軸上的實例。除此之外,我還需要爲最小值,最大值和實例分別着色繪圖線。

我一直在搞亂情,xyplot等等一段時間,但現在通常會缺少某些東西,讓它成爲「色彩」,「所有在一個圖表」等東西。

如果有人能夠解釋這個問題,那就太棒了。

+0

我認爲最快的解決方法是使用標準化數據的基本繪圖,然後手動添加y軸 – clemlaflemme

+0

您可以給我們您的「頭(數據)」? – TheBiro

回答

0

我與包plotly一樣。

這裏是代碼(我的數據不是根據您的描述,但它足以顯示代碼)。

library("plotly") 

set.seed(123) 

df <- data.frame(
    Cycle=c(1:10) 
    , Instances=runif(10,0,100) 
    , MaxFitness=runif(10,0.5,5) 
    , MinFitness=runif(10,0.5,5) 
) 


###### 
## Plot 
p <- plot_ly(data = df) %>% 
    add_lines(x=~Cycle, y=~MaxFitness, color="blue", name="MaxFitness") %>% 
    add_lines(x=~Cycle, y=~MinFitness, color="green", name="MinFitness") %>% 
    add_lines(x=~Cycle, y=~Instances, color="red", name="Instances", yaxis="y2") %>% 

    layout(

    title = "Double Y Axis Example", 
    yaxis=list(
     title = "left y axis" 
    ), 
    yaxis2=list(
     tickfont = list(color = "red"), 
     overlaying = "y", 
     side = "right", 
     title = "right y axis" 
    ) 

) 
p 

下面是輸出: enter image description here

現在,您可以根據自己的喜好編輯您的整個陰謀。

0

沒有任何MWE,我已經創建了一個:

# Create two series with different scales 
# Your Cycle is then the index, from 1 to 20 
x1 = runif(20, 0, 100) 
x2 = runit(20, 0.5, 5) 
# Plot them and add specific y axis 
plot(x1/100, type ="l", yaxt ="n", ylab ="", ylim = c(0,1)) 
lines((x2-0.5)/4.5, col = 2) 
axis(side = 2, at = c(0.2, 0.8), labels = c(20, 80)) 
axis(side = 4, at = c(0.5), labels = c(2.75)) 

所以結果是enter image description here