如何使用ggplot2设置两个x轴和两个y轴 [英] how to set two x axis and two y axis using ggplot2

查看:1129
本文介绍了如何使用ggplot2设置两个x轴和两个y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据如下:

d <- data.frame(X=c('x1','x2','x3','x1','x2','x3','x1','x2','x3'),
                Y=c('y1','y1','y1','y2','y2','y2','y3','y3','y3'),
                Value=c(1,2,1,3,1,4,3,5,2))

我使用以下代码生成热图:

I use the following code to generate a heat map:

ggplot(d,aes(x=X,y=Y,fill=Value)) +
  geom_tile() +
  scale_fill_gradient2(low='green',mid='white',high='red',midpoint=3) +
  theme(axis.text.x = element_text(angle = 90),
        axis.text = element_text(size = 10),
        panel.background = element_blank()) +
  labs(x='',y='')

图形为:

但是,我的整个数据约为500 * 500 ,因此x轴和y轴上的刻度线过多,因此标签无法识别清楚地。但是我必须保留所有值。

But, my whole data is around 500*500, so too many ticks on x axis and y axis so that the labels are impossible to recognize clearly. But I have to keep all the values.

所以我想使用x轴和y轴。例如,底部的x轴仅保留标签x1,x3,x5 ...,顶部的x轴仅保留标签x2,x4,x6...。然后对y轴进行相同操作。然后,图形如下所示:

So I want to use double x axis and double y axis. For example, the bottom x axis only keeps the lables x1, x3, x5... and the top x axis only keeps the labels x2, x4, x6.... Then I do the same to y axis. Then, the graph looks like:

我知道scale_x_continuous和sec.axis可能做这个。但是我的x和y是离散的。

I know scale_x_continuous and sec.axis might do this. But my x and y are discrete.

有人可以帮助我,使图看起来像第二个吗?

Could anyone help me so that the graph looks like the second one?

推荐答案

您是正确的,您只能使用带有连续数据的辅助轴。一种解决方案是将x和y值转换为连续值以进行绘制(只需使用整数序列),但将离散的x和y值另存为矢量以用于标记轴。

You are right than you can only use a secondary axis with continuous data. One solution is to convert your x and y values to continuous values for plotting (just use a sequence of integers), but save your discrete x and y values as vectors to use for labelling your axes.

在这里,我以一种可以扩展到较大的实际数据集的方式分配了连续值。我已经为x和y制作了关键数据帧,以使每个唯一离散值与整数匹配,然后将这些关键数据帧与原始数据帧合并,以便为每个离散x和y值分配一个可用于绘图的数值。

Here I've assigned continuous values in a way that should be scalable to your larger, actual dataset. I've made key dataframes for x and y to match each unique discrete value with an integer, than merged these key dataframes with your original dataframe so each discrete x and y value is assigned a numeric value that you can use for plotting.

#create key dataframes to assign an integer to each x and y value
key.df.x <- data.frame(X = unique(d$X), x.num = (1:length(unique(d$X))))
key.df.y <- data.frame(Y = unique(d$Y), y.num = (1:length(unique(d$Y))))

#merge key dataframes with original data
d <- merge(d, key.df.x, by = "X", all.x = TRUE)
d <- merge(d, key.df.y, by = "Y", all.x = TRUE)

#make label vectors from original variable names
xlabels = unique(d$X)
ylabels = unique(d$Y)

#select odd numbered elements for primary labels, even for secondary labels
xlabels.primary <- xlabels[seq(1, length(xlabels), by = 2)]
xlabels.secondary <- xlabels[seq(2, length(xlabels), by = 2)]
ylabels.primary <- ylabels[seq(1, length(ylabels), by = 2)]
ylabels.secondary <- ylabels[seq(2, length(ylabels), by = 2)]

ggplot(d,aes(x = x.num, y = y.num,fill=Value)) + #plot using continuous data
  geom_tile() +
  scale_fill_gradient2(low='green',mid='white',high='red',midpoint=3) +
  theme(axis.text.x = element_text(angle = 90),
        axis.text = element_text(size = 10),
        panel.background = element_blank()) +
  # set primary axis breaks to odd numbers, label with ylabels.primary
  scale_y_continuous(breaks = seq(1, max(d$y.num), by = 2), labels = ylabels.primary,
  # set secondary axis breaks to even numbers, label with ylabels.secondary                    
                      sec.axis = dup_axis(breaks = seq(2, max(d$y.num), by = 2),
                                         labels = ylabels.secondary)) +
  scale_x_continuous(breaks = seq(1, max(d$x.num), by = 2), , labels = xlabels.primary,
                     sec.axis = dup_axis(breaks = seq(2, max(d$x.num), by = 2),
                                         labels = xlabels.secondary)) +
  labs(x='',y='')

这篇关于如何使用ggplot2设置两个x轴和两个y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆