我想知道如何添加回归线方程和R ^ 2 ggplot
.我的代码是
library(ggplot2)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()
p
Run Code Online (Sandbox Code Playgroud)
任何帮助将受到高度赞赏.
我想用以下代码在图的最后一个面上注释一些文本:
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)
p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5)
print(p)
Run Code Online (Sandbox Code Playgroud)
但是这段代码注释了每个方面的文本.如果您指导我如何仅在一个方面获得带注释的文本,我将非常感激.提前致谢.
我想在ggplot2中的堆积条形图上显示数据值.这是我尝试过的代码
Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data <- data.frame(Year, Category, Frequency)
library(ggplot2)
p <- qplot(Year, Frequency, data = Data, geom = "bar", fill = Category, theme_set(theme_bw()))
p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position = "stack")
Run Code Online (Sandbox Code Playgroud)
我想在每个部分的中间显示这些数据值.在这方面的任何帮助将受到高度赞赏.谢谢
我希望rgl
在我的.Rnw
文档中包含动画图表knitr
.这是我的MWE:
\documentclass{article}
<< label = setup, include = FALSE>>=
opts_chunk$set(fig.path = 'figure/', cache = FALSE, dev = "pdf", fig.align = 'center', fig.show = 'hold', fig.width = 3, fig.height = 3, echo = TRUE, warning = FALSE, message = FALSE, size = 'footnotesize', comment=NA, results='hold')
knit_hooks$set(par = function(before, options, envir){
if (before && options$fig.show!='none')
par(mar = c(4, 4, 0.1, 0.1), cex.lab = 0.95, cex.axis = 0.9, mgp = c(2, 0.7, 0), tcl = -0.3)
} …
Run Code Online (Sandbox Code Playgroud) 这有可能用ggplot2重现这个格子图吗?
library(latticeExtra)
data(mtcars)
x <- t(as.matrix(scale(mtcars)))
dd.row <- as.dendrogram(hclust(dist(x)))
row.ord <- order.dendrogram(dd.row)
dd.col <- as.dendrogram(hclust(dist(t(x))))
col.ord <- order.dendrogram(dd.col)
library(lattice)
levelplot(x[row.ord, col.ord],
aspect = "fill",
scales = list(x = list(rot = 90)),
colorkey = list(space = "left"),
legend =
list(right =
list(fun = dendrogramGrob,
args =
list(x = dd.col, ord = col.ord,
side = "right",
size = 10)),
top =
list(fun = dendrogramGrob,
args =
list(x = dd.row,
side = "top",
size = 10))))
Run Code Online (Sandbox Code Playgroud)
当我运行时出现以下错误shiny
:
Error: Graphics API version mismatch
Listening on http://127.0.0.1:3774
Warning: Error in Cairo: Graphics API version mismatch
[No stack trace available]
Run Code Online (Sandbox Code Playgroud)
知道如何解决它。
我的会话信息如下:
sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] …
Run Code Online (Sandbox Code Playgroud) 我已经编写了一些R
使用S4类的函数.
现在我想用R
这些函数构建一个包.
我该怎么办?有什么我应该做的不同,因为我使用过S4课程吗?
我想知道是否有可能用ggplot2绘制pca biplot结果.假设我想用ggplot2显示以下双标图结果
fit <- princomp(USArrests, cor=TRUE)
summary(fit)
biplot(fit)
Run Code Online (Sandbox Code Playgroud)
任何帮助将受到高度赞赏.谢谢
使用arrange
函数dplyr
,我们可以按升序或降序排列行.想知道如何按自定义顺序排列行.请参阅MWE.
Reg <- rep(LETTERS[1:3], each = 2)
Res <- rep(c("Urban", "Rural"), times = 3)
set.seed(12345)
Pop <- rpois(n = 6, lambda = 500000)
df <- data.frame(Reg, Res, Pop)
df
Reg Res Pop
1 A Urban 500414
2 A Rural 500501
3 B Urban 499922
4 B Rural 500016
5 C Urban 501638
6 C Rural 499274
df %>%
arrange()
Run Code Online (Sandbox Code Playgroud)
期望的输出
Reg Res Pop
5 C Urban 501638
6 C Rural 499274
1 A Urban 500414
2 …
Run Code Online (Sandbox Code Playgroud) 我想在ggplot2中制作热图.我的玩具数据和代码是:
set.seed(12345)
dat <-
data.frame(
Row = rep(x = LETTERS[1:5], times = 10)
, Col = rep(x = LETTERS[1:10], each = 5)
, Y = rnorm(n = 50, mean = 0, sd = 1)
)
library(ggplot2)
p <- ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y), colour = "white") +
scale_fill_gradient(low = "white", high = "steelblue")
p
Run Code Online (Sandbox Code Playgroud)
我希望为这样的范围值设置颜色方案:
-3 <= Y < -2 ---> Dark Blue
-2 <= Y < -1 ---> Blue
-1 <= …
Run Code Online (Sandbox Code Playgroud)