2017-02-09 16 views
2

私はRプログラミングの新人で、何日かの間2-y軸のプロットを苦労しています。最後に、クライアントが尋ねたチャットを作成することはほとんどできませんでした。私の2行はどちらも同じ色で、私が望んでいなかったものです。R.ggplot2。 2行の色を2-Y軸プロットで区別できるように変更する方法

私は自分のニーズに合わせてインターネットで見つかった共通のスクリプトを使用しました。私が理解できない唯一のことは、異なる色の行を作る方法とggplot()関数を変更しないことです。それは最終的なプロットのために結合された凡例を生成するからです(もし行を特定の色にし、 "オブジェクトはいずれのプロットのグロブの一部でもなく、Rはエラーを返す)。

私は次のスクリプトを使用して3-5行のプロットを作成し、すぐに解決策を見つける必要があるので、被験者の助けを借りて/ありがとうと思います。

スクリプト:

library(ggplot2) 
library(zoo) 
library(gtable) 
library(grid) 

p4.1prod <-ggplot(data = p4_ch2_prod, aes (x = date, y = productivity, colour = productivity)) + 
    #Производительность труда, 2013=100 
    geom_line(size = 1)+ 
    labs(x=NULL, y=NULL)+ 
    scale_x_yearqtr(breaks = seq(from = min(p4_ch2_prod$date), to = max(p4_ch2_prod$date), by=0.25), format="Q%q %Y", expand=c(.01,0)) + 
    scale_y_continuous(breaks = seq(85,105,5), expand = c(0,0), limits = c(85,105)) + #setting the domain of the scale 

    theme(axis.text.x = element_text(angle = 90, vjust = .5, size = 5, colour = "black"), 
     axis.text.y = element_text(size = 5, colour = "black"), 
     panel.grid.major.y = element_line(colour = "#EDEDED", linetype = 2, size = .2), 
     panel.grid.minor.y = element_blank(), 
     panel.grid.major.x = element_line(colour = "#EDEDED", size = .2), 
     panel.grid.minor = element_blank(), 
     panel.background = element_rect(fill = "transparent", colour = NA), 
     #text = element_text(family="Times New Roman"), 
     axis.line.y = element_line(colour=NA), 
     axis.line.x=element_line(colour="#ABABAB"), 
     axis.ticks.length = unit(0,"cm"), 
     legend.position = "bottom", 
     legend.direction = "horizontal", 
     legend.title = element_blank(), 
     legend.spacing.x = unit(-.2,"cm"), 
     legend.key = element_blank(), 
     legend.key.height = unit(0, "cm"), 
     legend.text = element_text(size = 5), 
     legend.box.margin = unit (c(-3,1,1,1), "mm"), 
     plot.margin = unit (c(1,0,0,0), "mm")) 

p4.1prod 


p4.1ret <- ggplot(data = p4_ch2_ret, aes (x = date, y=return, colour = "Отдача на капитал, % (правая ось)"))+ 
    geom_line(size = 1)+ #, colour = "#DDCC77" 
    labs(x=NULL, y=NULL)+ 
    scale_x_yearqtr(breaks = seq(from = min(p4_ch2_ret$date), to = max(p4_ch2_ret$date), by=0.25), format="Q%q %Y", expand=c(.01,0)) + 
    scale_y_continuous(breaks = seq(-15,30,5), expand = c(0,0), limits = c(-15,30)) +  
    theme(axis.text.x = element_text(angle = 90, vjust = .5, size = 5, colour = "black"), 
     axis.text.y = element_text(size = 5, colour = "black"), 
     #panel.grid.major.y = element_line(colour = "#EDEDED", linetype = 2, size = .3), 
     #panel.grid.minor.y = element_line(colour = "transparent", size = NA), 
     #panel.grid.major.x = element_line(colour = "transparent", size = NA), 
     #panel.grid.minor = element_line(colour = "transparent", size = NA), 
     panel.background = element_rect(fill = NA), 
     panel.grid = element_blank(), 
     #text = element_text(family="Times New Roman"), 
     axis.line.y = element_line(colour=NA), 
     #axis.line.x=element_line(colour="#ABABAB"), 
     axis.ticks.length = unit(0,"cm"), 
     legend.position = "bottom", 
     legend.direction = "horizontal", 
     legend.title = element_blank(), 
     legend.spacing.x = unit(-.2,"cm"), 
     legend.key = element_blank(), 
     legend.key.height = unit(0, "cm"), 
     legend.text = element_text(size = 5), 
     legend.box.margin = unit (c(-3,1,1,1), "mm"), 
     plot.margin = unit (c(1,0,0,0), "mm")) 

p4.1ret 

g1 <- ggplot_gtable(ggplot_build(p4.1prod)) 
g2 <- ggplot_gtable(ggplot_build(p4.1ret)) 


pp <- c(subset(g1$layout, name == "panel", se = t:r)) 
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
        pp$l, pp$b, pp$l) 
# axis tweaks 
ia <- which(g2$layout$name == "axis-l") 
ga <- g2$grobs[[ia]] 
ax <- ga$children[[2]] 
ax$widths <- rev(ax$widths) 
ax$grobs <- rev(ax$grobs) 
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") 
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) 
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b) 


leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]] 
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]] 

g$grobs[[which(g$layout$name == "guide-box")]] <- 
            gtable:::cbind_gtable(leg1, leg2, "first") 
grid.draw(g) 


CairoPDF("plot4.pdf", width=5.23622, height=2.83465) 
plot(g) 
#plot(double_axis_graph(p4.1prod,p4.1ret)) 

dev.off() 

データ

p4_ch2_prod = 
    structure(list(date = structure(c(2010, 2010.25, 2010.5, 2010.75, 
2011, 2011.25, 2011.5, 2011.75, 2012, 2012.25, 2012.5, 2012.75, 
2013, 2013.25, 2013.5, 2013.75, 2014, 2014.25, 2014.5, 2014.75, 
2015, 2015.25, 2015.5, 2015.75, 2016, 2016.25, 2016.5), class = "yearqtr"), 
    productivity = c(86.367867684263, 88.3830463018648, 90.9947166315911, 
    93.0890082484875, 93.8358406567316, 95.415134362968, 95.1392324436027, 
    96.7490649309384, 97.2808331641485, 98.8920648452802, 98.6420849202174, 
    98.9827317138762, 100.118567582808, 99.3807124879942, 100.452579021256, 
    100.048140907942, 101.361645059966, 102.177365696465, 102.220265124015, 
    102.166598060618, 100.880500645703, 99.362767134256, 99.3229078598405, 
    98.7607969743729, 98.8417873432402, 99.153222798328, 98.2951748458741 
    )), .Names = c("date", "productivity"), row.names = c(NA, 
27L), class = "data.frame") 

p4_ch2_ret = 
structure(list(date = structure(c(2010, 2010.25, 2010.5, 2010.75, 
2011, 2011.25, 2011.5, 2011.75, 2012, 2012.25, 2012.5, 2012.75, 
2013, 2013.25, 2013.5, 2013.75, 2014, 2014.25, 2014.5, 2014.75, 
2015, 2015.25, 2015.5, 2015.75, 2016, 2016.25, 2016.5), class = "yearqtr"), 
    return = c(11.7652500842395, 20.1068362958603, 24.4189393391774, 
    27.7603749135956, 22.6909560844267, 19.7512448106456, 9.65853487280893, 
    5.84667147280881, 4.23826276745065, 3.70105654292071, 5.21839712971448, 
    3.46196244414108, 6.08000307844456, -0.0813936964316113, 
    2.57919252646666, 0.206819314044496, 1.70263155186845, 5.21025899295173, 
    3.06177589390397, 4.16006634923697, -4.69833971033449, -10.8930467624443, 
    -11.4996955087892, -14.091528184568, -12.0277510178615, -7.46002412972934, 
    -11.475198069944)), .Names = c("date", "return"), row.names = c(NA, 
27L), class = "data.frame") 
+2

ようこそ! _minimal_と_reproducible_ [example](http://stackoverflow.com/questions/5963269)を表示することを忘れないでください。 – Axeman

+0

'dput(p4_ch2_prod)'と 'dput(p4_ch2_ret)'の結果を提供できますか? – Wietze314

+0

最初の投稿に追加されました。 – dkolkin

答えて

1

scale_colour_identity()あなたが探しているものである可能性があります。私はコードの末尾に# Hereコメントの変更をマークしました。私はy軸のラベルも着色しました。必要ない場合は黒に戻してください。

また、凡例を組み合わせると、結合凡例の中央に1 nullの幅があり、その結果、凡例が広すぎる(私の意見では)。より良い間隔のためにゼロに設定してください。

library(ggplot2) 
library(zoo) 
library(gtable) 
library(grid) 

p4.1prod <- ggplot(data = p4_ch2_prod, aes (x = date, y = productivity, colour = "blue")) + # Here 
    #Производительность труда, 2013=100 
    geom_line(size = 1)+ 
    scale_colour_identity(guide = "legend", label = "Something blue") + # Here 
    labs(x=NULL, y=NULL)+ 
    scale_x_yearqtr(breaks = seq(from = min(p4_ch2_prod$date), to = max(p4_ch2_prod$date), by=0.25), format="Q%q %Y", expand=c(.01,0)) + 
    scale_y_continuous(breaks = seq(85,105,5), expand = c(0,0), limits = c(85,105)) + #setting the domain of the scale 

    theme(axis.text.x = element_text(angle = 90, vjust = .5, size = 5, colour = "black"), 
     axis.text.y = element_text(size = 5, colour = "blue"), # Here 
     panel.grid.major.y = element_line(colour = "#EDEDED", linetype = 2, size = .2), 
     panel.grid.minor.y = element_blank(), 
     panel.grid.major.x = element_line(colour = "#EDEDED", size = .2), 
     panel.grid.minor = element_blank(), 
     panel.background = element_rect(fill = "transparent", colour = NA), 
     #text = element_text(family="Times New Roman"), 
     axis.line.y = element_line(colour=NA), 
     axis.line.x=element_line(colour="#ABABAB"), 
     axis.ticks.length = unit(0,"cm"), 
     legend.position = "bottom", 
     legend.direction = "horizontal", 
     legend.title = element_blank(), 
     legend.spacing.x = unit(-.2,"cm"), 
     legend.key = element_blank(), 
     legend.key.height = unit(0, "cm"), 
     legend.text = element_text(size = 5), 
     legend.box.margin = unit (c(-3,1,1,1), "mm"), 
     plot.margin = unit (c(1,0,0,0), "mm")) 

p4.1prod 


p4.1ret <- ggplot(data = p4_ch2_ret, aes (x = date, y=return, colour = "red"))+ # Here 
    geom_line(size = 1)+ #, colour = "#DDCC77" 
    scale_colour_identity(guide = "legend", labels = c("Something red")) + # Here 
    labs(x=NULL, y=NULL)+ 
    scale_x_yearqtr(breaks = seq(from = min(p4_ch2_ret$date), to = max(p4_ch2_ret$date), by=0.25), format="Q%q %Y", expand=c(.01,0)) + 
    scale_y_continuous(breaks = seq(-15,30,5), expand = c(0,0), limits = c(-15,30)) +  
    theme(axis.text.x = element_text(angle = 90, vjust = .5, size = 5, colour = "black"), 
     axis.text.y = element_text(size = 5, colour = "red"), # Here 
     #panel.grid.major.y = element_line(colour = "#EDEDED", linetype = 2, size = .3), 
     #panel.grid.minor.y = element_line(colour = "transparent", size = NA), 
     #panel.grid.major.x = element_line(colour = "transparent", size = NA), 
     #panel.grid.minor = element_line(colour = "transparent", size = NA), 
     panel.background = element_rect(fill = NA), 
     panel.grid = element_blank(), 
     #text = element_text(family="Times New Roman"), 
     axis.line.y = element_line(colour=NA), 
     #axis.line.x=element_line(colour="#ABABAB"), 
     axis.ticks.length = unit(0,"cm"), 
     legend.position = "bottom", 
     legend.direction = "horizontal", 
     legend.title = element_blank(), 
     legend.spacing.x = unit(-.2,"cm"), 
     legend.key = element_blank(), 
     legend.key.height = unit(0, "cm"), 
     legend.text = element_text(size = 5), 
     legend.box.margin = unit (c(-3,1,1,1), "mm"), 
     plot.margin = unit (c(1,0,0,0), "mm")) 

p4.1ret 

g1 <- ggplotGrob(p4.1prod) 
g2 <- ggplotGrob(p4.1ret) 


pp <- c(subset(g1$layout, name == "panel", se = t:r)) 
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
        pp$l, pp$b, pp$l) 
# axis tweaks 
ia <- which(g2$layout$name == "axis-l") 
ga <- g2$grobs[[ia]] 
ax <- ga$children[[2]] 
ax$widths <- rev(ax$widths) 
ax$grobs <- rev(ax$grobs) 
# ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") # Here - This line not needed 
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) 
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b) 


leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]] 
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]] 

leg = gtable:::cbind_gtable(leg1, leg2, "first")    # Here 
leg$widths[5:6] = unit(0, "cm")        # Here 

g$grobs[[which(g$layout$name == "guide-box")]] <- leg 

grid.draw(g) 
+0

サンディー、ありがとう!これはまさに私が必要としていたものです。 – dkolkin

2

私はあなたが達成したいかわからないんだけど、ここでlatticeとソリューションです。私はまた、必要ではないが、2つのy軸に異なった色をつけた。

library(lattice) 
library(latticeExtra) 


set.seed(1) 
x = rnorm(10) 
y1 = rnorm(10) 
y2 = rnorm(10) 

obj1 = xyplot(y1~x, type = c("p", "a"), ylab.right="") 
obj2 = xyplot(y2~x, type = c("p", "a")) 
doubleYScale(obj1, obj2, text = c("y1", "y2"), 
      add.ylab2 = TRUE) 

enter image description here

+1

私はOPが明らかにプロットの外見を微調整する作業をしているので、これを 'ggplot'と説明することを検討すべきだと思います。 – Wietze314

+0

私は同意します。私はggplot2の使い方を学び、実験して結果を得るのにほぼ3日間費やしました。だから、ggplot2の助けが大いに評価されるでしょう。 – dkolkin

関連する問題