无论图形有多大,都会在X轴上使用固定比例的R平行坐标绘图 [英] R parallel coordinate plot with fixed scale on X-axis, no matter how large the plot becomes

查看:140
本文介绍了无论图形有多大,都会在X轴上使用固定比例的R平行坐标绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图<在R中建立平行坐标图,以显示不同年龄段的排名差异。我想在Y轴上有一个固定的比例来显示数值。

以下是一个PC图:



目标是看到线条的斜度真的很好。所以,如果我的价值1与价值1000绑定,我想看到这条线急剧下降。

到目前为止,如果我的值太大,我的情节就会全部压扁,所以一切都很合适,很难直观地看到任何东西。



到目前为止,绘制平行坐标图的代码如下:

  pc_18_34 < -  read.table(parCoordData_18_24_25_34.csv,header = FALSE,sep =\ t)
#数据帧的名称列
colnames(pc_18_34)= c 18-25,25-34)

#build平行坐标图
#doc:http://docs.ggplot2.org/current/geom_path.html
group < - rep(c(Top 10,Top 10-29,Top 30-49),each = 18)
df< - data.frame(id = seq_along( (df)[3] =18-25
colnames(df)[4] =25-34,group,pc_18_34 [,1],pc_18_34 [,2])
colnames
library(reshape2)#for melt

dfm< - melt(df,id.var = c(id,group))
dfm [order (dfm $ group,dfm $ ArtistRank,递减= TRUE),]
colnames(dfm)[3] =AgeGroup
colnames(dfm)[4] =ArtistRank

ggplot(dfm,aes(x = AgeGroup,y = ArtistRank,group = id,color = group),main =Tops across age groups)+ geom_path(alpha = 0.5,size = 1)+ geom_path(aes (颜色=克roup))

我已经研究过如何使用ggplot等库来改变比例尺但是当我有一层规模的时候,图表甚至不再显示。



关于如何制作使用固定比例的任何想法(比如在图中显示为5px的等级1的差异)​​,即使这意味着情节很高



Thaanks !! :)

解决方案

您可以根据轴断点的数量将面板高度设置为绝对大小。请注意,设备不会自动缩放,因此您必须手动调整以适应您的情节。

  library(ggplot2)
library(gtable)

p < - ggplot(Loblolly,aes(height,factor(age)))+
geom_point()

gb < - ggplot_build(p)
gt < - ggplot_gtable(gb)
n < - length(gb $ panel $ ranges [[1]] $ y.major_source)

#在gtable布局中找到面板
面板< - gt $ layout $ t [grepl(panel,gt $ layout $ name)]
#将新高度赋给面板,基于中断次数
gt $ heights [panel]< - list(unit(n * 25,pt))
grid.newpage()
grid.draw (gt)


I am trying to build a parallel coordinate diagram in R for showing the difference in ranking in different age groups. And I want to have a fixed scale on the Y axis for showing the values.

Here is a PC plot :

The goal is to see the slopes of the lines really well. So if I have value 1 that is bound with the value 1000, I want to see the line going aaall the way down steeply.

In R so far, if I have values that are too big, my plot is all squished so everything fits and it's hard to visualize anything.

My code for drawing the parallel coordinate plot is the following so far:

pc_18_34 <- read.table("parCoordData_18_24_25_34.csv", header=FALSE, sep="\t")
#name columns of data frame
colnames(pc_18_34) = c("18-25","25-34")

#build the parallel coordinate plot
# doc : http://docs.ggplot2.org/current/geom_path.html
group <- rep(c("Top 10", "Top 10-29", "Top 30-49"), each = 18)
df <- data.frame(id = seq_along(group), group, pc_18_34[,1], pc_18_34[,2])
colnames(df)[3] = "18-25"
colnames(df)[4] = "25-34"
library(reshape2) # for melt

dfm <- melt(df, id.var = c("id", "group"))
dfm[order(dfm$group,dfm$ArtistRank,decreasing=TRUE),]
colnames(dfm)[3] = "AgeGroup"
colnames(dfm)[4] = "ArtistRank"

ggplot(dfm, aes(x=AgeGroup, y=ArtistRank, group = id, colour = group), main="Tops across age groups")+  geom_path(alpha = 0.5, size=1) + geom_path(aes(color=group))

I have looked into how to get the scales to change in ggplot, using libraries like scales but when I had a layer of scale, the diagram doesn't even show up anymore.

Any thoughts on how to make to use a fixed scale (say difference of 1 in rank shown as 5px in the plot), even if it means that the plot is very tall ?

Thaanks !! :)

解决方案

You can set the panel height to an absolute size based on the number of axis breaks. Note that the device won't scale automatically, so you'll have to adjust it manually for your plot to fit well.

library(ggplot2)
library(gtable)

p <- ggplot(Loblolly, aes(height, factor(age))) +
  geom_point()

gb <- ggplot_build(p)
gt <- ggplot_gtable(gb)
n <- length(gb$panel$ranges[[1]]$y.major_source)

# locate the panel in the gtable layout
panel <- gt$layout$t[grepl("panel", gt$layout$name)]
# assign new height to the panels, based on the number of breaks
gt$heights[panel] <- list(unit(n*25,"pt"))
grid.newpage()
grid.draw(gt)

这篇关于无论图形有多大,都会在X轴上使用固定比例的R平行坐标绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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