我想使用 ggplot2 绘制一些不同的数据项,使用两种不同的色标(一种是连续的,一种是从两个不同的 df 中分离出来的)。我可以按照我想要的方式单独绘制,但我不能让它们一起工作。似乎您不能在同一个情节中使用两种不同的色阶?我看过类似的问题 here和 here ,这让我相信我想要实现的目标在 ggplot2 中是不可能的,但以防万一我错了,我想说明我的问题,看看是否有解决方法。
我有一些 GIS 流数据,其中附加了一些分类属性,我可以绘制这些数据(下面代码中的 p1
)以获得:
我还有一组具有连续响应的位置,我也可以绘制(下面代码中的 p2
)以获得:
但是我不能将两者结合起来(下面代码中的 p3
)。我收到这个错误
Error in scales[[prev_aes]] : attempt to select less than one element
注释掉该行
scale_colour_hue("Strahler order") +
将错误更改为Error: Discrete value supplied to continuous scale
基本上,ggplot2 似乎对
geom_path
使用相同的比例类型(连续或离散)。调用和geom_point
调用。所以当我传递离散变量时,factor(Strahler)
, 到 scale_colour_gradientn
规模,情节失败。有没有办法解决这个问题?如果有
data
那就太棒了scales 函数的参数,告诉它应该从哪里映射或设置属性。这甚至可能吗?非常感谢和可重现的代码如下:
library(ggplot2)
### Download df's ###
oldwd <- getwd(); tmp <- tempdir(); setwd(tmp)
url <- "http://dl.dropbox.com/u/44829974/Data.zip"
f <- paste(tmp,"\\tmp.zip",sep="")
download.file(url,f)
unzip(f)
### Read in data ###
riv_df <- read.table("riv_df.csv", sep=",",h=T)
afr_df <- read.table("afr_df.csv", sep=",",h=T)
vil_df <- read.table("vil_df.csv", sep=",",h=T)
### Min and max for plot area ###
xmin <- -18; xmax <- 3; ymin <- 4; ymax <- 15
### Plot river data ###
p1 <- ggplot(riv_df, aes(long, lat)) +
geom_map( mapping = aes( long , lat , map_id = id ) , fill = "white" , data = afr_df , map = afr_df ) +
geom_path( colour = "grey95" , mapping = aes( long , lat , group = group , size = 1 ) , data = afr_df ) +
geom_path( aes( group = id , alpha = I(Strahler/6) , colour = factor(Strahler) , size = Strahler/6 ) ) +
scale_alpha( guide = "none" ) +
scale_colour_hue("Strahler order") +
scale_x_continuous( limits = c( xmin , xmax ) , expand = c( 0 , 0 ) ) +
scale_y_continuous( limits = c( ymin , ymax ) , expand = c( 0 , 0 ) ) +
coord_map()
print(p1) # This may take a little while depending on computer speed...
### Plot response data ###
p2 <- ggplot( NULL ) +
geom_point( aes( X , Y , colour = Z) , size = 2 , shape = 19 , data = vil_df ) +
scale_colour_gradientn( colours = rev(heat.colors(25)) , guide="colourbar" ) +
coord_equal()
print(p2)
### Plot both together ###
p3 <- ggplot(riv_df, aes(long, lat)) +
geom_map( mapping = aes( long , lat , map_id = id ) , fill = "white" , data = afr_df , map = afr_df ) +
geom_path( colour = "grey95" , mapping = aes( long , lat , group = group , size = 1 ) , data = afr_df ) +
geom_path( aes( group = id , alpha = I(Strahler/6) , colour = factor(Strahler) , size = Strahler/6 ) ) +
scale_colour_hue("Strahler order") +
scale_alpha( guide = "none" ) +
scale_x_continuous( limits = c( xmin , xmax ) , expand = c( 0 , 0 ) ) +
scale_y_continuous( limits = c( ymin , ymax ) , expand = c( 0 , 0 ) ) +
geom_point( aes( X , Y , colour = Z) , size = 2 , shape = 19 , data = vil_df ) +
scale_colour_gradientn( colours = rev(heat.colors(25)) , guide="colourbar" ) +
coord_map()
print(p3)
#Error in scales[[prev_aes]] : attempt to select less than one element
### Clear-up downloaded files ###
unlink(tmp,recursive=T)
setwd(oldwd)
干杯,
西蒙
最佳答案
你可以这样做。您需要告诉网格图形将一个图叠加在另一个图上。您必须获得边距和间距等,完全正确,并且您必须考虑顶层的透明度。总之……不值得。以及可能使情节困惑。
但是,我认为有些人可能会喜欢有关如何实现这一目标的指针。注意我用过 code from this gist使顶部图中的元素透明,因此它们不会使下面的元素不透明:
grid.newpage()
pushViewport( viewport( layout = grid.layout( 1 , 1 , widths = unit( 1 , "npc" ) ) ) )
print( p1 + theme(legend.position="none") , vp = viewport( layout.pos.row = 1 , layout.pos.col = 1 ) )
print( p2 + theme(legend.position="none") , vp = viewport( layout.pos.row = 1 , layout.pos.col = 1 ) )
看我的回答 here了解如何将图例添加到网格布局的另一个位置。
关于r - 在同一个 ggplot 中绘制离散和连续尺度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508902/