我试图弄清楚如何在 ggplot2 中的堆叠条形图上指定轮廓颜色。在下面的代码中,我指定了color="green",它为每个条形提供了绿色轮廓。我想为每个条形指定不同的轮廓颜色(例如,cut=Fair将填充黄色并用橙色勾勒轮廓,cut=Good将填充浅绿色并用深绿色轮廓轮廓等)。
ggplot(diamonds) +
geom_bar(aes(clarity, fill=cut))+
scale_fill_manual(values=c("Fair"="yellow","Good"="light green","Very Good"="light blue","Premium"="pink","Ideal"="purple"))+
Run Code Online (Sandbox Code Playgroud)
我尝试过scale_color_manual()在美学中指定颜色向量geom_bar(),但都没有成功。
我在循环中生成了几个条形图,它们都根据输出大小(从绘图/设备大小假设?)而不是根据条形大小调整大小。这意味着具有两个条形的图具有粗条,而具有 6 个条的图具有细条;不过,两个输出的大小相同。下面的代码表示我的脚本具有可复制的数据(我对我的做了许多其他 aes/主题更改)。
我希望输出图调整大小(在条形宽度的维度上),以便条形在不同的图形中始终具有相同的宽度,但输出图像根据(相同宽度)条形的数量改变大小。
my_factors = c("vs","cyl","carb")
for (current_factor in my_factors) {
c <- ggplot(mtcars, aes(factor(current_factor)))
c + geom_bar() + coord_flip()
ggsave(paste0(my_factors(current_factor),".png")
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果我错过了一些明显的东西,我是 ggplot 和 R 的新手。我来自 MATLAB,所以整个“设备”的事情仍然让我感到困惑!在 MATLAB 中,我会明确指定条形大小(即不是相对的),并且输出会相应地调整大小。
这是我的barplot外观的一个简单示例:
x <- data.frame(aa=c(0.2,0.6,0.1), dd = c(1,2,3))
x <- melt(x, "dd")
y <- data.frame(bb=c(0.4,0.5), dd = c(1,2))
y <- melt(y, "dd")
z <- data.frame(cc=c(0.5,0.25,0.1,0.05), dd = c(1,2,3,4))
z <- melt(z, "dd")
x=rbind(x,y,z)
col=c("white","grey","blue","white","red","white","green","blue","green")
ggplot(x, aes(x = variable, y = value)) + geom_bar(stat = "identity", fill = col)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以使我的条形圆角末端变圆,例如使用lineline选项(http://sape.inf.usi.ch/quick-reference/ggplot2/lineend)?
有没有办法用geom_line绘制geom_bar,如下图所示.
我想出了两个独立的图表.如何将它们分别与左右两侧的两个不同轴组合.
library(ggplot2)
temp = data.frame(Product=as.factor(c("A","B","C")),
N = c(17100,17533,6756),
n = c(5,13,11),
rate = c(0.0003,0.0007,0.0016),
labels = c(".03%",".07%",".16%"))
p1 = ggplot(data = temp, aes(x=Product,y=N))+
geom_bar(stat="identity",fill="#F8766D")+geom_text(aes(label=n,col="red",vjust=-0.5))+
theme(legend.position="none",axis.title.y=element_blank(),axis.text.x = element_text(angle = 90, hjust = 1))
p1
p2 = ggplot(data = temp,aes(x=Product,y=rate))+
geom_line(aes(group=1))+geom_text(aes(label=labels,col="red",vjust=0))+
theme(legend.position="none",axis.title.y=element_blank(),
axis.text.x = element_text(angle = 90, hjust = 0))+
xlab("Product")
p2
Run Code Online (Sandbox Code Playgroud)
非常感谢.
我什至不知道如何描述我试图正确生成的情节,这不是一个好的开始。我将首先向您展示我的数据,然后尝试解释/展示包含数据元素的图像。
我的数据:
strain condition count.up count.down
1 phbA balanced 120 -102
2 phbA limited 114 -319
3 phbB balanced 122 -148
4 phbB limited 97 -201
5 phbAB balanced 268 -243
6 phbAB limited 140 -189
7 phbC balanced 55 -65
8 phbC limited 104 -187
9 phaZ balanced 99 -28
10 phaZ limited 147 -205
11 bdhA balanced 246 -159
12 bdhA limited 143 -383
13 acsA2 balanced 491 -389
14 acsA2 limited 131 -295
Run Code Online (Sandbox Code Playgroud)
我有七个样本,每个样本有两种情况。对于这些样本中的每一个,我都有下调的基因数量和上调的基因数量(count.down 和 count.up)。
我想绘制此图,以便对每个样本进行分组;所以 …
StudentData <- data.frame(gender = sample( c("male","female"), 100, replace=TRUE),
degree = sample( c("Associates", "Masters", "PhD"), 100, replace=TRUE),
category = sample( c("Audit", "Credit"), 100, replace=TRUE))
Run Code Online (Sandbox Code Playgroud)
在下面的数据集中,我尝试创建一个条形图,绘制具有副学士学位、硕士学位或博士学位的样本的百分比,按性别分隔(通过使用 facet_grid() 完成)。这是我到目前为止生成的:
StudentData %>% ggplot(., aes(x=degree, group=gender)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
Run Code Online (Sandbox Code Playgroud)
但是,我还想将每个图表上的“审计”和“信用”组之间的差异显示为并排条形。然而,当我将“fill=category”添加到 ggplot 的美学中时,没有任何变化:
StudentData %>% ggplot(., aes(x=degree, group=gender, fill=category)) +
geom_bar(aes(y=..prop..), stat="count", position=position_dodge()) +
geom_text(aes(label=scales::percent(round(..prop..,2)),
y=..prop..), stat="count", vjust=-.5) +
scale_y_continuous(limits=c(0,1),labels = scales::percent) +
ylab("Percent of Sample") +
facet_grid(~gender)
Run Code Online (Sandbox Code Playgroud)
我意识到这通常是使用完成的geom_bar(stat="identity", …
我无法在geom_plot中删除多余的空白侧翼组的酒吧。
我想做Roland在这里实现的目标:删除ggplot2条之间的空间,但是当我尝试实现他的解决方案时,出现错误“警告消息:
geom_bar()不再具有binwidth参数。请geom_histogram()改为使用。”
我将这行代码添加到绘图中(尝试不同的宽度):
geom_histogram(binwidth = 0.5) +
Run Code Online (Sandbox Code Playgroud)
它返回“错误:stat_bin()不能出于审美目的使用。” 而且没有情节。
数据:
mydf<- data.frame(Treatment = c("Con", "Con", "Ex", "Ex"),
Response = rep(c("Alive", "Dead"), times=2),
Count = c(259,10,290,21))
aPalette<-c("#009E73", "#D55E00")
Run Code Online (Sandbox Code Playgroud)
情节:
example<-ggplot(mydf, aes(factor(Response), Count, fill = Treatment)) +
geom_bar(stat="identity",position = position_dodge(width = 0.55), width =
0.5) +
scale_fill_manual(values = aPalette, name = "Treatment") + #legend title
theme_classic() +
labs(x = "Response",
y = "Count") +
scale_y_continuous(breaks = c(0,50,100,150,200,250,275), expand = c(0,0),
limits = c(0, 260)) …Run Code Online (Sandbox Code Playgroud) 我有这个数据:
table(main$Sex1,main$District)
Bahawalnagar Barkhan Chiniot Faisalabad Ghotki
Female 37 16 26 97 46
Male 25 19 15 20 25
Run Code Online (Sandbox Code Playgroud)
我可以用基础 R 绘制它
barplot(table(main$Sex1,main$District))
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,我怎样才能用 ggplot2 做到这一点?谢谢
我想使用ggplot2在R中创建一个条形图,这样条形图就是透明的,允许背景图像可见,而图形的其余部分是不透明的并覆盖背景图像.
我可以将图像添加到背景中,如下所示,但我找不到只在条形图中显示背景图像的方法.基本上,我希望创建我在这里的反转.
library(ggplot2)
library(jpeg)
library(grid)
library(scales)
montage <- readJPEG("AcanthMontage.jpg")
mont <- rasterGrob(montage,
width = unit(1,"npc"),
height = unit(1,"npc"))
montplot <- ggplot(frequencyDF, aes(x=depth, y= perLiter)) +
annotation_custom(mont, -Inf, Inf, -Inf, Inf) +
scale_fill_continuous(guide = FALSE) +
geom_bar(stat = "identity", color="black", fill="white", alpha=0.5) +
coord_flip() +
scale_y_continuous(limits= c(0,1.25), expand = c(0, 0)) +
scale_x_continuous(limits= c(-1000,0), expand = c(0,0)) +
theme_bw() +
theme(text=element_text(size=16)) +
xlab("Depth (m)") +
ylab("Cells per Liter")
montplot
Run Code Online (Sandbox Code Playgroud)

我有一些数据可以捕获多个时间段内两个不同组的百分比。
df <- structure(list(period = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L), .Label = c("FY18 Q4", "FY19 Q1", "FY19 Q2", "FY19 Q3",
"FY19 Q4"), class = "factor"), key = c("You", "You", "You", "You", "You",
"Me", "Me", "Me", "Me", "Me"), value = c(0.707036316472114,
0.650424585523655, 0.629362214199759, 0.634016393442623, 0.66578947368421,
0.509574110529601, 0.505703612591682, 0.493109917284898, 0.497505296695832,
0.523938932489946)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
我想绘制此数据,以使一段时间内的两个条形图相互重叠,但是条形图的宽度不同。我希望“ Me”的标准是width=0.5“ You”的标准width=0.7。我还想包括一个说明每种颜色代表什么的图例。
如果要并排绘制条形图,可以使用position="dodge",如下所示:
library(ggplot2)
library(dplyr)
ggplot(data=df, aes(x=period, y=value, fill=key)) + …Run Code Online (Sandbox Code Playgroud)