几个小时以来,我一直在挠头。我现在所拥有的:
library(ggplot2)
library(grid)
all_data = data.frame(country=rep(c("A","B","C","D"),times=1,each=20),
value=rep(c(10,20,30,40),times=1,each=20),
year = rep(seq(1991,2010),4))
# PLOT GRAPH
p1 <- ggplot() + theme_bw() + geom_line(aes(y = value, x = year,
colour=country), size=2,
data = all_data, stat="identity") +
theme(plot.title = element_text(size=18,hjust = -0.037), legend.position="bottom",
legend.direction="horizontal", legend.background = element_rect(size=0.5, linetype="solid", colour ="black"),
legend.text = element_text(size=16,face = "plain"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(),axis.line = element_line(colour = "black"),legend.title = element_blank(),
axis.text=element_text(size=18,face = "plain"),axis.title.x=element_text(size=18,face = "plain", hjust = 1,
margin = margin(t = 10, r = …Run Code Online (Sandbox Code Playgroud) 我尝试在 ggplot2 库中添加 ggrepel 来绘制图表:
set.seed(42)
ggplot(mtcars) +
geom_point(aes(wt, mpg), size = 5, color = 'grey') +
geom_label_repel(aes(wt, mpg, fill = factor(cyl), label = rownames(mtcars)),
fontface = 'bold', color = 'white',
box.padding = 0.35, point.padding = 0.5,
segment.color = 'grey50') +
theme_classic(base_size = 16)
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Run Code Online (Sandbox Code Playgroud)Error in convertUnit(x, unitTo, "x", "dimension", "x", "dimension", valueOnly = valueOnly) : 'x' argument must be a unit object
谢谢?
我在 R 中创建了两个帕累托图表,都使用相同的数据。一个使用 ggplots stat_pareto,另一个使用 qcc 库中的 pareto.chart 函数。
ggplot(DT4, aes(x = reorder(sap_object_type_desc, -sum_duration), y =
sum_duration)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=90,hjust=1)) +
stat_pareto(point.color = "red",
point.size = 2,
line.color = "black",
#size.line = 1,
bars.fill = c("blue", "orange"))
Run Code Online (Sandbox Code Playgroud)
或者使用pareto.chart函数
pareto.chart(avector,
ylab = "Sum",
# xlab = "Objective Type Description",
main = "Avector Pareto Chart",
cumperc = c(20,40,60,80,100)) # or = seq(0, 100, by =25)
Run Code Online (Sandbox Code Playgroud)
我想要做的是调整上面两个图上的第二个 y 轴,使 100% 累积百分比与最高条对齐,就像第三个例子一样。有什么建议?
我遇到的问题是,我希望高于某个阈值 (=<25) 的点不会产生大于设定比例的点。这些较大的点仍然需要显示,并且不能被排除:
d=data.frame(y=c(1,2,6,4,4,6,7,8),
x=c(8,4,7,5,4,9,2,3),
coverage=c(0,6,9,88,25,22,17,100),
col=c(0,.25,.50,.76,.80,1.00,.11,.34)
)
ggplot() +
scale_size(range = c(0, 13),
breaks = c(0, 5, 10, 20, 25),
labels = c("0", "5", "10", "20", "25+"),
guide = "legend"
) +
geom_point(data = d, mapping = aes(x = x, y = y, color = col, size = coverage)) +
labs(title = "geom_point")
Run Code Online (Sandbox Code Playgroud)
在上面的示例代码中,我有两个点的“覆盖范围”大于 25+ 并且超出了范围。我希望这些点的大小与 25+ 阈值的大小相同。
我有一个 ggplot 存储在 say 中p,希望添加一个poly_stat_eq图层来显示曲线方程。我想更改文本的字体大小,但找不到有关如何实现它的文档
p +
stat_poly_eq(formula = y ~ x,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
label.x = 4, label.y = -5, parse = TRUE)
Run Code Online (Sandbox Code Playgroud) 我需要可视化的数据包含 4 个车站之间的火车上的乘客负载。数据以有序的方式提供,即。A站和V站之间的火车上有432名乘客,V站和B站之间有543名乘客,依此类推。在编码示例中,我使用 ggplot2 绘制了数据。
library(tidyverse)
df <- tribble(~Station, ~Passengers,
"A", 432,
"V", 543,
"B", 435,
"Q", 0)
df$Station <- factor(df$Station, levels=unique(df$Station))
ggplot(data = df, aes(x = Station, y = Passengers)) +
geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)
问题:我想将 x 轴刻度和车站名称放置在条形之间。目标是将条形向右移动 50%。
library(tidyverse)
ggplot(mtcars) +
geom_bar(aes(x = factor(cyl))) +
scale_y_continuous(expand = expand_scale(mult = c(0, 0)))
Run Code Online (Sandbox Code Playgroud)
我的问题似乎是 ggplotexpand_scale()的行为不一致。但这种说法可能是不正确的。让我们从上面的图作为我们的基线开始并深入研究。
如果我正确理解了这个论点,mult = c(X, Y)我就可以将 ggplot 扩展到图下方的 X% 和图上方的 Y%。这就是我用下面的代码得到的。
ggplot(mtcars) +
geom_bar(aes(x = factor(cyl))) +
scale_y_continuous(expand = expand_scale(mult = c(1, 0)))
Run Code Online (Sandbox Code Playgroud)
ggplot(mpg %>% filter(displ > 6, displ < 8), aes(displ, cty)) +
geom_point() +
facet_grid(vars(drv), vars(cyl)) +
geom_text(aes(label = trans)) +
scale_x_continuous(expand = c(0, 0)) +
coord_cartesian(clip = "off")
Run Code Online (Sandbox Code Playgroud)
这是我想为示例三和示例四解决的下一个基线。
ggplot(mpg %>% …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ggplot2 对地图进行分面,因为它比 tmap 包更快。遗憾的是我得到的地图并不是我想要的。
library(ggplot2)
library(raster)
library(ggspatial)
chile <- getData("GADM",country="Chile",level=1)
chile2= chile[c(2,4:5,7,8,12:16),]
chile2$grupo=1
chile3= chile[c(1,3,6,9:11),]
chile3$grupo=2
mapa=rbind(chile2, chile3)
ggplot() +
layer_spatial(mapa) +
lims(x = c( -77.1,-65), y = c(-57, -15))+
facet_wrap(~grupo)
Run Code Online (Sandbox Code Playgroud)
遗憾的是上面的地图不是我需要的。通过 tmap,我得到了下面的地图,这正是我真正需要的地图:
你知道如何使用ggplot2解决这个问题吗?
我正在尝试在 3 x 4 网格上绘制 12 个不同的图。但是,它只绘制了最后一个 12 次。谁能帮我?我实在受够了。谢谢
library(ggplot2)
library(gridExtra)
pmax=0.85
K_min = 0.0017
T = seq(100,1200,by=100) ## ISIs
lambda =1/T
p=list()
for(i in (1:length(lambda))){
p[[i]]<-ggplot(data.frame(x = c(0, 1)), aes(x = x)) +
stat_function(fun = function (x) (lambda[i]*(1-(1-pmax))/K_min)*(1-x)^((lambda[i]/K_min)-1)*
(1-(1-pmax)*x)^-((lambda[i]/K_min)+1),colour = "dodgerblue3")+
scale_x_continuous(name = "Probability") +
scale_y_continuous(name = "Frequency") + theme_bw()
main <- grid.arrange(grobs=p,ncol=4)
}
Run Code Online (Sandbox Code Playgroud)
这段代码生成了正确的图片,但我需要使用 ggplot,因为我的其他数字都在 ggplot 中。
par( mfrow = c( 3, 4 ) )
for (i in (1:length(lambda))){
f <- function (x) ((lambda[i]*(1-(1-pmax))/K_min)*(1-x)^((lambda[i]/K_min)-1)*
(1-(1-pmax)*x)^-((lambda[i]/K_min)+1) )
curve(f,from=0, to=1, …Run Code Online (Sandbox Code Playgroud) 我需要从日期中提取两位数的天数。
例如:
date <- as.Date(01-01-2016)
Run Code Online (Sandbox Code Playgroud)
year(date) 会给 '2016'month(date) 会产生“1”day(date) 会返回“1”我想要month()并day()返回 '01'
标准的 %D 或 %M 在这里似乎不起作用。任何建议,将不胜感激!