简体   繁体   English

在r中指定颜色和标签轴的散点图

[英]scatter plot specifying color and labelling axis in r

I have following data and plot: 我有以下数据和图表:

pos <- rep(1:2000, 20)
xv =c(rep(1:20, each = 2000))
# colrs <- unique(xv)
colrs <- xv # edits 
yv =rnorm(2000*20, 0.5, 0.1)

xv   = lapply(unique(xv), function(x) pos[xv==x])
to.add = cumsum(sapply(xv, max) + 1000)

bp <- c(xv[[1]], unlist(lapply(2:length(xv), function(x) xv[[x]] + to.add[x-1])))
plot (bp,yv, pch = "*", col = colrs)

在此处输入图片说明

I have few issues in this plot I could not figure out. 我在这个图中没有几个问题,无法弄清楚。

(1) I want to use different color for different group or two different color for different groups (ie xv), but when I tried color function in terms to be beautiful mixture. (1)我想对不同的组使用不同的颜色,或者对不同的组使用两种不同的颜色(即xv),但是当我尝试用颜色函数来达到漂亮的混合效果时。 Although I need to highlight some points (for example bp 4000 to 4500 for example with blue color) 尽管我需要突出显示一些点(例如,用蓝色将bp 4000到4500突出显示)

(2) Instead of bp positions I want to put a tick mark and label with the group. (2)我不想在bp位置放置一个勾号,并在组中添加标签。

Thank you, appreciate your help. 谢谢,感谢您的帮助。

Edits: with help of the following answer (with slight different approach in case I have unbalanced number in each group will work) I could get the similar plot. 编辑:在以下答案的帮助下(如果我在每个组中有不平衡的数字,则略有不同的方法将起作用),我可以获得相似的图。 But still question remaining regarding colors is what if I want to use two alternate colors in alternate group ? 但是仍然有关于颜色的疑问,如果我要在替代组中使用两种替代颜色该怎么办?

You can solve your colour issue by repeating the colour index however many times each group has a point plotted, like so: 您可以通过重复颜色索引来解决颜色问题,但是每组多次绘制一个点,如下所示:

plot (bp,yv, pch = "*", col = rep(colrs,each=2000))

The default colour palette (see ?palette or palette() ) will wrap around itself and you might want to specify your own to get 20 distinct colours. 默认调色板(请参阅?palettepalette() )将环绕其自身,您可能需要指定自己的palette()以获取20种不同的颜色。

To relabel the x axis, try plotting without the axis and then specifying the points and labels manually. 要重新标记x轴,请尝试在不使用x轴的情况下进行绘制,然后手动指定点和标签。

plot (bp,yv, pch = "*", col = rep(colrs,each=2000),xaxt="n")
axis(1,at=seq(1000,58000,3000),labels=1:20)

If you are trying to squeeze a lot of labels in there, you might have to shrink the text ( cex.axis )or spin the labels 90 degrees ( las=2 ). 如果要在其中挤压很多标签,则可能必须缩小文本( cex.axis )或将标签旋转90度( las=2 )。

plot (bp,yv, pch = "*", col = rep(colrs,each=2000),xaxt="n")
axis(1,at=seq(1000,58000,3000),labels=1:20,cex.axis=0.7,las=2)

Result: 结果:

在此处输入图片说明

One way is you could use a nested ifelse . 一种方法是您可以使用嵌套的ifelse
I'm still learning R , but one way it could be done would look something like: 我仍在学习R ,但是可以完成的一种方法是:

plot(whatev$x, whatev$y, col=ifelse(xv<2000,red,ifelse(2000<xv & xv<4000,yellow,blue)))  

You could nest as many of these as you want to have specificity on the colors and the intervals. 您可以根据需要嵌套任意数量的颜色和间隔。 The ifelse command is of form ifelse(TEST, True, False) . ifelse命令的形式为ifelse(TEST, True, False)

A simpler way would be to use the unique groups in xv to assign rainbow colors. 一种更简单的方法是使用xv中的唯一组分配彩虹色。

colrs=rainbow(length(unique(xv)))  #Or colrs=rainbow(length(xv)) if xv is unique.
plot(whatev$x, whatev$y, col=colrs)

I hope I got all that right. 我希望我没事。 I'm still learning R myself. 我自己还在学习R。

I'm going to go out on a limb and guess that your real data are something like 2000 values of things from 20 different groups. 我要冒昧地猜测一下,您的真实数据就像来自20个不同组的2000值一样。 For instance, heights of 2000 plants of 20 different species. 例如,20种不同物种的2000种植物的高度。 In such a case, you might want to look at the dotplot() function (or as illustrated below, dotplot.table() ) in the lattice package. 在这种情况下,您可能想要查看lattice包中的dotplot()函数(或如下图所示的dotplot.table() )。

Generate matrix of hypothetical values: 生成假设值矩阵:

set.seed(1)

myY <- sapply( seq_len(20), function(x) rnorm(2000, x^(1/3)))

Transpose matrix to get groups as rows 转置矩阵以将组作为行

myY <- t(myY)

Provide names of groups to matrix: 提供矩阵的组名:

dimnames(myY)[[1]]<-paste("group", seq_len(nrow(myY)))

Load lattice package 负载lattice包装

library(lattice)

Generate dotplot 生成点图

dotplot(myY, horizontal = FALSE, panel = function(x, y, horizontal, ...) {
  panel.dotplot(x = x, y = y, horizontal = horizontal, jitter.x = TRUE,
    col = seq_len(20)[x], pch = "*", cex = 1.5)
  }, scales = list(x = list(rot = 90))
)

Which looks like (with unfortunate y-axis labeling): 看起来像(带有不幸的y轴标签):

点图

Seeing that @JohnCLK is requesting a way of colouring by values on the x axis, I tried these demos in ggplot2 -- each uses a dummy variable that is coded based on values or ranges to be highlighted in the other variables. 看到@JohnCLK正在请求一种通过x轴上的值进行着色的方法,我尝试了ggplot2这些演示-每个ggplot2都使用一个虚拟变量,该虚拟变量根据要在其他变量中突出显示的值或范围进行编码。

So, first set up the data, as in the question: 因此,首先要设置数据,如下所示:

pos <- rep(1:2000, 20)
xv <- c(rep(1:20, each = 2000))
yv <- (2000*20, 0.5, 0.1)
xv <- lapply(unique(xv), function(x) pos[xv==x])
to.add <- cumsum(sapply(xv, max) + 1000)
bp <- c(xv[[1]], unlist(lapply(2:length(xv), function(x) xv[[x]] + to.add[x-1])))

Then load ggplot2 , prepare a couple of utility functions, and set the default theme: 然后加载ggplot2 ,准备几个实用程序功能,并设置默认主题:

library("ggplot2")

make.png <- function(p, fName) {
    png(fName, width=640, height=480, units="px")
    print(p)
    dev.off()
}

make.plot <- function(df) {
    p <- ggplot(df, 
                aes(x = bp,
                    y = yv, 
                    colour = highlight))
    p <- p + geom_point()
    p <- p + opts(legend.position = "none")
    return(p)
}

theme_set( theme_bw() )

Draw a plot which highlights values in a defined range on the vertical axis: 绘制一个图表,在垂直轴上突出显示定义范围内的值:

# highlight a horizontal band
df <- data.frame(cbind(bp, yv))
df$highlight <- 0
df$highlight[ df$yv >= 0.4 & df$yv < 0.45 ] <- 1
p <- make.plot(df)
print(p)
make.png(p, "demo_horizontal.png")

水平带

Next draw a plot which highlights values in a defined range on the x axis, a vertical band: 接下来绘制一个图表,突出显示x轴(垂直带)上定义范围内的值:

# highlight a vertical band
df$highlight <- 0
df$highlight[ df$bp >= 38000 & df$bp < 42000 ] <- 1
p <- make.plot(df)
print(p)
make.png(p, "demo_vertical.png")

垂直带

And finally draw a plot which highlights alternating vertical bands, by x value: 最后绘制一个以x值突出显示交替垂直带的图:

# highlight alternating bands
library("gtools")
alt.band.width <- 2000
df$highlight <- as.integer(df$bp / alt.band.width)
df$highlight <- ifelse(odd(df$highlight), 1, 0)
p <- make.plot(df)
print(p)
make.png(p, "demo_alternating.png")

交替带

Hope this helps; 希望这可以帮助; it was good practice anyway. 无论如何,这是个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM