我用来egg对齐页面上的多个绘图。我想知道是否可以通过标题a)而c)不是绘图区域来对齐两列?谢谢!
代码:
library(egg)
library(grid)
p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + ggtitle("a)")
p1
p2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + facet_wrap(~ cyl, ncol = 2, scales = "free") +
guides(colour = "none") +
theme() + ggtitle("b)")
p2
p3 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
geom_point() + facet_grid(. ~ am, scales = "free") + guides(colour="none") +
ggtitle("c)")
p3
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g3 …Run Code Online (Sandbox Code Playgroud) 在新的R会话中加载了这个.我无法弄清楚出了什么问题.
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap( ~ cyl)
Run Code Online (Sandbox Code Playgroud)
错误:
Error: `predicate` must be a closure or function pointer
ls()
character(0)
Run Code Online (Sandbox Code Playgroud)
会话信息:
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.2.1.9000
loaded via a namespace (and not attached):
[1] colorspace_1.3-2 scales_0.5.0.9000 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用定义的配色方案绘制栅格,该配色方案是从 Rcolorbrewer 包中获取的,到目前为止没有问题。raster\xc2\xb4s 值范围从 0 到 1,没有 NA\xc2\xb4s。
\n\nlibrary(RColorBrewer)\nlibrary(classInt)\n\npal <- brewer.pal(n=50, name = "RdYlGn")\nplot(rw_start_goal_stan, col=pal)\nRun Code Online (Sandbox Code Playgroud)\n\n
现在我尝试包含分位数分隔符,这是我使用 ClassInt 包计算的
\n\nlibrary(RColorBrewer)\nlibrary(classInt)\n\npal <- brewer.pal(n=50, name = "RdYlGn")\nbreaks.qt <- classIntervals(rw_start_goal_stan@data@values, style = "quantile")\nplot(rw_start_goal_stan, breaks = breaks.qt$brks, col=pal)\nRun Code Online (Sandbox Code Playgroud)\n\n错误的是,plot() 仅将配色方案应用于值范围的 50%,其余部分保持白色。
\n\n
我究竟做错了什么?
\n我正在绘制一个 y 变量与多个 x 变量的关系图。我有一个使用 lapply 的工作解决方案。但是,我无法将 x 变量的名称写为每个图的 x 标签。这是我所拥有的一个简化示例:
目标是针对每个 x 变量绘制 y 变量,从而生成三个图,并将每个 x 变量的名称添加为 x 轴标签。
生成一个包含 1 个 y 变量和 3 个 x 变量的数据框:
df <- data.frame(y.variable=c(11:20), x1=c(21:30),x2=c(1:10),x3=c(31:40))
Run Code Online (Sandbox Code Playgroud)
一个应该以字符串形式检索变量名称的函数:
get_name <- function(v1) {deparse(substitute(v1))}
Run Code Online (Sandbox Code Playgroud)
生成 y 相对于 x 变量的图的函数:
generate_plot <- function(x.variable) {ggplot(data = df, aes(x.variable, y.variable )) +geom_point() + xlab(get_name(variable.name))}
Run Code Online (Sandbox Code Playgroud)
调用 lapply 对 df 的每一列执行generate_plot:
lapply(df, generate_plot)
Run Code Online (Sandbox Code Playgroud)
这会产生三个图,每个图都将“variable.x”作为其 x 标签,而不是所需的变量名称 x1、x2 和 x3。
我正在尝试使用Linux Mint下的标志--enable-R-shlib从源代码安装R 3.5.0。在没有标志的情况下进行配置和安装可以正常工作,但是RStudio要求设置标志。但是,运行时
./configure --enable-R-shlib
make
Run Code Online (Sandbox Code Playgroud)
我得到错误
/usr/bin/ld: CommandLineArgs.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
CommandLineArgs.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Makefile:177: recipe for target 'libR.so' failed
make[3]: *** [libR.so] Error 1
make[3]: Leaving directory '/home/hps/Downloads/R-3.5.0/src/main'
Makefile:135: recipe for target 'R' failed
make[2]: *** [R] Error 2
make[2]: Leaving directory '/home/hps/Downloads/R-3.5.0/src/main'
Makefile:28: recipe for target 'R' failed
make[1]: *** [R] Error 1
make[1]: …Run Code Online (Sandbox Code Playgroud) 我正在尝试生成 10 对图,每页图有几对,并使用循环for来构造这些图对。但是,这些图会作为单独的图而不是页面发送到设备。
下面的 MWE 对于基础图形和ggplot版本具有相同的结构,但基础图形可以工作,也ggplot可以不工作。我需要做什么才能使第二个版本中的分页正确?
library(ggplot2)
attach(mtcars)
# correct configuration
par(mfrow=c(2,2))
for (ii in 1:3){
vars <- c("wt", "disp", "wt")
plot(get(vars[ii]), mpg)
hist(get(vars[ii]))
}
# places each on separate plot
par(mfrow=c(2,2))
for (ii in 1:3){
vars <- c("wt", "disp", "wt")
p <- ggplot(mtcars, aes(get(vars[ii]), mpg)) + geom_point(size=4)
plot(p)
p <- ggplot(mtcars, aes(get(vars[ii]))) + geom_histogram()
plot(p)
}
detach(mtcars)
Run Code Online (Sandbox Code Playgroud) 我在R中有多个级别列表有问题.这是我的数据结构的一个小例子.
library(purrr)
> example
[[1]]
[[1]][[1]]
id.value id.name value
1 2 Tim -1.68956
2 4 Jack 1.23950
3 5 Mary -0.10897
4 3 Joseph -0.11724
5 1 Kermit 0.18308
[[1]][[2]]
id.value id.name value
1 6 Tim 0.50381
2 2 Jack 2.52834
3 1 Mary 0.54910
4 4 Joseph 0.23821
5 5 Kermit -1.04889
6 3 Red 1.29476
[[1]][[3]]
id.value id.name value
1 4 Tim -0.47279
2 1 Jack -1.06782
3 2 Mary -0.21797
4 3 Joseph -1.02600
5 5 Kermit …Run Code Online (Sandbox Code Playgroud) 使用下面的代码,刻面 bB 中的标签未正确定位。问题似乎源于这样一个事实,即没有position_dodge(preserve="single")for geom_text(对吗?)。我知道我可以“手动”添加一个空的虚拟单元格(在 facet bB 中填充 y=0),但我想知道是否有任何方法可以直接在 ggplot 中纠正它?
v1 <- LETTERS[1:2]
v2 <- letters[1:2]
v3 <- c("x","y")
g <- expand.grid(v1,v2,v3)
val=c(sample(10,8))
df<- data.frame(g,val)
df<- df[-8,]
df %>% ggplot() +
geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3),
stat="identity",
position=position_dodge(preserve="single"))+
geom_text(aes(x=Var2, y=val+1, label=val, group=Var3),
position=position_dodge(width=1))+
facet_grid(Var1~Var2, scale="free_x")
Run Code Online (Sandbox Code Playgroud)
更新/答案:使用 position_dodge2 将条形与标签对齐(但是,条形居中且未与其他方面中的条形对齐)。
df %>% ggplot() +
geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3), stat="identity",
position=position_dodge2(preserve="single"))+
geom_text(aes(x=Var2, y=val+1, label=val, group=Var3),
position=position_dodge2(width=1))+
facet_grid(Var1~Var2, scale="free_x")
Run Code Online (Sandbox Code Playgroud) 我的情节是可行的,除了我无法更改字体系列。即使我可以更改其他内容(例如颜色,大小和对齐方式),它也始终保持默认值。
这是我的代码:
ggplot(data = SeattleJuly17Data,
aes(x = Price, y = SatisfactionScore, col = RoomType)) +
geom_point() +
xlim(0,500) +
geom_smooth() +
ggtitle("Satisfaction Trends by Price and Room Type") +
theme(plot.title = element_text(family = "Calibri",
size=15,
color="Red",
hjust = 0.5)) +
xlab("Price per Night") +
ylab("Guest Satisfaction Score")
Run Code Online (Sandbox Code Playgroud) 我使用mpgggplot2 包中的数据来测试绘图参数。我发现 lengend.key 背景(右上角,标签车辆模型)颜色可以更改为任何其他颜色,但透明或白色除外。如何将lengend键背景默认灰色更改为透明?
library(ggplot2)
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=class))+
geom_point(aes(size=displ),alpha=0.5)+
stat_smooth(method='loess')+
scale_size_continuous(range=c(4,10))+
facet_wrap(~year,ncol=2)+
labs(x='distance per gallon in city',y='distance per gallon in highway',
title='fuel consumption and model',size='displacement',colour='vehicle model')
p+
theme(
plot.background=element_rect(fill='transparent', colour='transparent'),
panel.background=element_rect(fill='transparent', colour='gray'),
panel.border=element_rect(fill='transparent', colour='black'),
axis.text=element_text(color='black'),
panel.grid.major=element_line(colour='grey',linetype='dashed'),
strip.background=element_rect(fill='transparent', colour='transparent'),
panel.spacing.x=unit(4,'mm'),
legend.box.background=element_rect(fill='blue', colour='transparent'),
legend.key=element_rect(fill='transparent', colour='transparent')
)
Run Code Online (Sandbox Code Playgroud)