你能帮我用ggplot2包和mat矩阵中的数据创建下面的条形图吗?
mat <- matrix(c(70.93,78.58,78.72,69.24,62.53,43.85,83.49,70.00,78.30,78.11,71.16,63.82,47.37,89.87),ncol=2)
colnames(mat) <- c("Crude","Standardized")
rownames(mat) <- 2006:2012
library(gplots)
library(RColorBrewer)
my_palette <- palette(brewer.pal(7,"Set1"))
barplot2(mat,
main="Crude and Standardized Rates",
xlab="Type", ylab="Rate", xlim=c(0,20), ylim=c(40,100),
col=my_palette, beside=TRUE, plot.grid = TRUE, xpd=FALSE)
legend(locator(1), rownames(mat), title ="Year",fill=my_palette)
Run Code Online (Sandbox Code Playgroud)

我有一个由以下R代码生成的图 - 基本上是许多直方图/条的面板.我希望每一个都添加一条垂直线,但每个方面的垂直线在它的位置上是不同的.或者,我想根据x值是否高于阈值将条纹颜色设置为红色 - 如何使用ggplot2/R对此进行绘图.
我这样生成了图表:
Histogramplot3 <- ggplot(completeFrame, aes(P_Value)) + geom_bar() + facet_wrap(~ Generation)
Run Code Online (Sandbox Code Playgroud)
在completeFrame是我的数据框架的地方,P_Value是我的x变量,而Facet Wrap Variable Generation是一个因素.
我想复制下面的图ggplot2(虽然更大,但我不知道为什么这里的图这么小)。
(本研究的主题是穆斯林少数民族青年的融入。)
我在 Stata 中使用 -coefplot- 包开发了上面的原始图,现在尝试在 R 中进行相同的操作。
在 R 和 中,我从使用(来自 Mplus 应用程序中的估计)ggplot2获得的两个简单数据帧开始。MplusAutomation可以使用以下代码重现数据帧:
resModel1 <-
structure(list(param = c("FEMALE", "CHRISTIAN", "MUSLIM"),
low2.5 = c(-0.436, 0.038, 0.19),
est = c(-0.271, 0.18, 0.354), up2.5 = c(-0.106, 0.323, 0.519)),
.Names = c("param", "low2.5", "est", "up2.5"), row.names = 7:9,
class = c("data.frame", "mplus.params"))
resModel2 <-
structure(list(param = c("FEMALE", "CHRISTIAN", "MUSLIM", "CHRISTGI", "MUSLIMGI"),
low2.5 = c(-0.672, -0.256, -0.018, 0.131, -0.143),
est = c(-0.437, -0.038, 0.237, 0.403, 0.237), up2.5 …Run Code Online (Sandbox Code Playgroud) 我正在尝试为每组绘制一个基本的条形图。
由于值非常大,我想显示每个条形(即组)的条形内每个组的百分比。
我设法显示了总数的百分比,但这不是我所期望的:在每个条形中,我希望 % 的总和等于 100%。
有没有一种简单的方法可以在不更改数据框的情况下做到这一点?
(DF <- data.frame( year = rep(2015:2017, each = 4),
Grp = c("Grp1", "Grp2", "Grp3", "Grp4"),
Value = trunc(rnorm(12, 2000000, 100000))) )
ggplot(DF) +
geom_bar(aes(x = year, y = Value, fill = Grp),
stat = "identity",
position = position_stack()) +
geom_text(aes(x = year, y = Value, group = Grp,
label = percent(Value/sum(Value))) ,
position = position_stack(vjust = .5))
Run Code Online (Sandbox Code Playgroud) 我有一个与拍号 (0-100) 相对应的角度数据 (0-360) 数据集,并使用 ggplot 创建散点图。
ggplot(kfaf)+
geom_point(aes(time, angle, color = condition), size = 1.5)
Run Code Online (Sandbox Code Playgroud)

我想做的是格式化 y 轴,使 0=360 并从 ymin = 270 读取到 ymax = 269,功能上移动图表,使 0/360 位于 y 轴的中间。找不到任何用于格式化圆形数据轴的内容,有帮助吗?
我的数据如下:
structure(list(Year = 1994:2016, Kcalpd = c(86L, 91L, 98L, 107L,
116L, 126L, 123L, 112L, 103L, 102L, 103L, 92L, 77L, 59L, 43L,
29L, 19L, 14L, 13L, 12L, 12L, 10L, 9L), Thtonnes = c(728.364,
757.467, 780.423, 792.756, 701.685, 720.71, 677.292, 761.649,
668.218, 679.042, 974.355, 1005.035, 1123.09, 1055.07, 1092.498,
1100.654, 899.767, 1018.462, 1046.096, 1084.173, 1158.217, 802.194,
276.773)), row.names = c(NA, -23L), class = "data.frame", .Names = c("Year",
"Kcalpd", "Thtonnes"))
Run Code Online (Sandbox Code Playgroud)
并且,我的代码如下:
scaleFactor <- max(wfd$Thtonnes) / max(wfd$Kcalpd)
ggplot(wfd, aes(x=Year)) +
geom_col(aes(y=Thtonnes), fill="blue") +
geom_col(aes(y=Kcalpd * …Run Code Online (Sandbox Code Playgroud) 我使用ggsurvplot和创建了两个图ggplot。下面是我尝试过的代码
p1 <- ggsurvplot(KM_new, data = data_log, time = "log_time", conf.int = FALSE)
p2 <- ggplot(data, aes(x = x, y = y)) +
geom_smooth()
Run Code Online (Sandbox Code Playgroud)
如何将 p1 和 p2 合并到一个图中?我可以分别获取这两个图,但无法获取单个图。
我正在使用 R 编程语言。
最近,我看到这篇文章How can i make a stacked multiple Density Plot with ggplot? 其中显示了一个非常有趣的图表:
我正在尝试学习如何复制该图。
我首先模拟了该图的数据:
text = c(
"Morena, pvem, pt 307[282-326] Actual 413",
"Morena, PT 263[244-280] Actual 303",
"Morena, pvem 265[243-282] Actual 267",
"PAN, PRI, PRD, MC 193 [167-211] Actual 163",
"PAN,PRI, PRD 180[155-199] Actual 137",
"PAN, PRI 152[131-167] Actual 126",
"PAN, PRD 112[95-125] Actual 89",
"PRI, PRF 97[83-111] Actual 59"
)
means = c(300,250,200,150,140,130,120,110)
data = data.frame()
for(i in seq_along(text)){
random_numbers = rnorm(100, means[i], 10) …Run Code Online (Sandbox Code Playgroud)