将多个ggplot2图与一个共同的x轴和不同的y轴对齐,每个轴都有不同的y轴标签

时间:2018-05-21 13:16:05

标签: r ggplot2 geom-bar

我想得到相同的图表,对于相同的轴x,两个轴y 不同。一个在右边(天数:天气数据集) 和左边的一个(物种数量)。

我已经制作了一个显示降水量,温度的图表 我的天气数据集。对此,在右边的y轴上,我 想添加物种数量的轴。有可能吗?

您可以在下面看到我的数据和我的代码:

# for i in range(int(batch_size-n_steps)+1):#total insert
    # for k in range(n_steps):
        # hidden1_copy_tmp=tf.slice(hidden1, [k+i, 0], [1, hidden1.shape[1]])
        # if hidden1_copy== None:
            # hidden1_copy=hidden1_copy_tmp
        # else:
            # hidden1_copy=tf.concat([hidden1_copy, hidden1_copy_tmp], 0) 

我为物种数量添加了structure(list(SOUNAME = c("BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)", "BALLYSHANNON (CATHLEENS FALL)" ), year_month = c("2014-05", "2014-05", "2014-05", "2014-05", "2014-06", "2014-06", "2014-06", "2014-06", "2014-07", "2014-07", "2014-07", "2014-07"), pre_type = c("NONE", "HEAVY", "LIGHT", "MEDIUM", "NONE", "HEAVY", "LIGHT", "MEDIUM", "NONE", "HEAVY", "LIGHT", "MEDIUM"), pre_value = c(3, 6, 20, 2, 16, 2, 9, 2, 3, 3, 22, 3), tem_type = c("V_COLD", "COLD", "HOT", "MEDIUM", "V_COLD", "COLD", "HOT", "MEDIUM", "V_COLD", "COLD", "HOT", "MEDIUM"), tem_value = c(0, 31, 0, 0, 0, 24, 6, 0, 0, 23, 8, 0), nb_species = c(NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), x = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L)), .Names = c("SOUNAME", "year_month", "pre_type", "pre_value", "tem_type", "tem_value", "nb_species", "x"), row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame")) ggplot(data = complet_b, aes(x = x, y = pre_value, fill = pre_type), stat = "identity") + scale_x_continuous(breaks=1:3, labels=unique(complet_b$year_month)) + geom_bar(stat = "identity", width=0.3) + xlab("date") + ylab ("Number of days of precipitation") + ggtitle("Precipitation per month") + labs(fill = "Frequency") + geom_bar(data=complet_b,aes(x=x+0.4, y=tem_value, fill=tem_type), width=0.3, stat = "identity") + xlab("date") + ylab("Number of days of temperature") + ggtitle("Temperature per month") + labs(fill = "Frequency") + geom_point( data = complet_b, aes( x= x, y = nb_species), stat = "identity") 函数,但我没有 知道如何在右边添加轴。

谢谢。

0 个答案:

没有答案