使用标准R图形将线性模型趋势线添加到箱线图时,我使用:
boxplot(iris[,2]~iris[,1],col="LightBlue",main="Quartile1 (Rare)")
modelQ1<-lm(iris[,2]~iris[,1])
abline(modelQ1,lwd=2)
Run Code Online (Sandbox Code Playgroud)
但是,在ggplot2中使用它时:
a <- ggplot(iris,aes(factor(iris[,1]),iris[,2]))
a + geom_boxplot() +
geom_smooth(method = "lm", se=FALSE, color="black", formula=iris[,2]~iris[,1])
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
geom_smooth: Only one unique x value each group.Maybe you want aes(group = 1)?
Run Code Online (Sandbox Code Playgroud)
这条线没有出现在我的情节中.
这两种情况中使用的模型是相同的.如果有人能指出我哪里出错了,那就太好了.
编辑:使用虹膜数据集作为示例.
我想知道是否有人有问题Matplotlib的盒子情节传单没有显示?
我把这个例子复制粘贴到python脚本中:http: //blog.bharatbhole.com/creating-boxplots-with-matplotlib/
...但是盒子图传单(异常值)没有显示.有谁知道为什么我可能不会看到它们?对不起,如果这是一个愚蠢的问题,但我不能为我的生活弄清楚为什么它不起作用.
## Create data
np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)
## combine these different collections into a list
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]
# Create a figure instance
fig = plt.figure(1, figsize=(9, 6))
# Create an axes instance
ax = fig.add_subplot(111)
# Create the boxplot
bp = ax.boxplot(data_to_plot)
Run Code Online (Sandbox Code Playgroud)
我也尝试添加showfliers=True到该脚本的最后一行,但它仍然无法正常工作.
这是我得到的输出:

我有代码创建一个boxplot,使用R中的ggplot,我想用年份和Battle标记我的异常值.
这是我创建箱图的代码
require(ggplot2)
ggplot(seabattle, aes(x=PortugesOutcome,y=RatioPort2Dutch ),xlim="OutCome",
y="Ratio of Portuguese to Dutch/British ships") +
geom_boxplot(outlier.size=2,outlier.colour="green") +
stat_summary(fun.y="mean", geom = "point", shape=23, size =3, fill="pink") +
ggtitle("Portugese Sea Battles")
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?我知道这是正确的,我只想标记异常值.
有没有办法在R中创建一个框图,它将在框中显示(某处)"N =(样本大小)"?varwidth logical根据样本大小调整框的宽度,但不允许在不同的图之间进行比较.
FWIW,我以下列方式使用boxplot命令,其中'f1'是一个因素:
boxplot(xvar ~ f1, data=frame, xlab="input values", horizontal=TRUE)
Run Code Online (Sandbox Code Playgroud) 我有一个数据集(测试),如下所示:
Type Met1 Met2 Met3 Met4
TypeA 65 43 97 77
TypeA 46 25 76 77
TypeA 44 23 55 46
TypeA 46 44 55 77
TypeA 33 22 55 54
TypeB 66 8 66 47
TypeB 55 76 66 65
TypeB 55 77 88 46
TypeB 36 67 55 44
TypeB 67 55 76 65
Run Code Online (Sandbox Code Playgroud)
我在盒子图上检查了很多链接,但是我仍然没有成功获得我想要的盒子图.我希望有一个箱形图,我的X轴有所有Mets(Met1,Met2,Met3,Met4)的A型(黄色,橙色).从本质上讲,我想要以下内容(取自此处):
我正在尝试一些事情,比如
boxplot(formula = len ~ Type , data = test, subset == "TypeA")
boxplot(formula = len ~ Type , data = …Run Code Online (Sandbox Code Playgroud) 如何在ggplot2中更改x轴标签的名称?见下文:
ggbox <- ggplot(buffer, aes(SampledLUL, SOC)) + geom_boxplot()
ggbox <- ggbox + theme(axis.text.x=element_text(color = "black", size=11, angle=30, vjust=.8, hjust=0.8))
ggbox<- ggbox + labs(title = "Land cover Classes") + ylab("SOC (g C/m2/yr)") + xlab("Land cover classes")
Run Code Online (Sandbox Code Playgroud)
上面的代码创建了下图:

我希望能够对这些类的第一个字母(即Crop,而不是crop)进行首映.
我已经尝试了下面的代码,但不知道在哪里放置它以及确切使用的功能.
labels = c("Citrus", "Crop", "Cypress Swamp", ..........)
(我正在使用Windows 7,Rstudio)
我有一个数据框,其中包含x轴数字箱和跨多个类别的连续y轴数据.最初,我通过制作x轴箱"因子"并绘制熔化数据的箱线图来创建箱线图.可重复的数据:
x <- seq(1,10,by=1)
y1 <- rnorm(10, mean=3)
y2 <- rnorm(10, mean=10)
y3<- rnorm(10, mean=1)
y4<- rnorm(10, mean=8)
y5<- rnorm(10, mean=12)
df <- data.frame(x,y1,y2,y3,y4,y5)
df.m <- melt(df, id="x")
Run Code Online (Sandbox Code Playgroud)
我的代码创建x轴数据作为一个因素:
df.m $ x < - as.factor(df.m $ x)
我的ggplot:
ggplot(df.m, aes(x=x, y=value))+
geom_boxplot(notch=FALSE, outlier.shape=NA, fill="red", alpha=0.1)+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Run Code Online (Sandbox Code Playgroud)
由此产生的情节:
:
问题是我不能使用x轴数字间距,因为x轴被分类为具有相等间距的因子.我希望能够使用像scale_x_continuous这样的东西来操纵轴断裂和间距,例如,间隔为2,而不是每1个盒子图,但是当我尝试用x轴绘制数据时"as.numeric ",我只得到所有数据的一个箱线图:

有什么方法可以获得这种连续观察的箱形图曲线(第一幅图像),同时仍能控制x轴的数字属性?谢谢!
我正试图在ggplot2中绘制一个简单的箱形图.我有物种丰富性与土地利用类.但是,我的数据中有2个NA.出于一些奇怪的原因,他们正在被绘制,即使他们被R理解为NA.任何建议要删除它们?
我正在使用的代码是:
ggplot(data, aes(x=luse, y=rich))+
geom_boxplot(mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", outlier.colour = "red", outlier.shape = 16, outlier.size = 2, notch = F, notchwidth = 0.5)+
scale_x_discrete("luse", drop=T)+
geom_smooth(method="loess",aes(group=1))
Run Code Online (Sandbox Code Playgroud)
但是,该图表包含2个NA用于luse.不幸的是我无法发布图片,但想象一下我的图表中添加了一个NA栏.
我想为连续x变量的组创建多个变量的箱线图.对于每组x,箱形图应该彼此相邻排列.
数据如下所示:
require (ggplot2)
require (plyr)
library(reshape2)
set.seed(1234)
x <- rnorm(100)
y.1 <- rnorm(100)
y.2 <- rnorm(100)
y.3 <- rnorm(100)
y.4 <- rnorm(100)
df <- as.data.frame(cbind(x,y.1,y.2,y.3,y.4))
Run Code Online (Sandbox Code Playgroud)
然后我融化了
dfmelt <- melt(df, measure.vars=2:5)
Run Code Online (Sandbox Code Playgroud)
这个解决方案 中显示的facet_wrap(ggplot(facets)中的因子多个绘图)给出了单个图中的每个变量,但是我希望每个变量的箱形图彼此相邻,每个x的bin都在一个图.
ggplot(dfmelt, aes(value, x, group = round_any(x, 0.5), fill=variable))+
geom_boxplot() +
geom_jitter() +
facet_wrap(~variable)
Run Code Online (Sandbox Code Playgroud)
这显示了y变量彼此相邻但不是bin x.
ggplot(dfmelt) +
geom_boxplot(aes(x=x,y=value,fill=variable))+
facet_grid(~variable)
Run Code Online (Sandbox Code Playgroud)
现在我想为x的每个bin生成这样的图.
什么必须改变或添加?
我正在尝试创建一个图形,其中 x 轴是字典的键,而箱线图来自字典中的信息。我的字典可以有很多键。
Data = {'ABC': [34.54, 34.345, 34.761], 'DEF': [34.541, 34.748, 34.482]}
for ID in Data:
plt.boxplot(Data[ID])
plt.xlabel(ID)
plt.savefig('BoxPlot.png')
plt.clf()
Run Code Online (Sandbox Code Playgroud)
然而,它似乎将箱线图放在一起。我尝试在没有运气的情况下迭代 boxplot 中的位置值。如果可能,我还想将键用作每个箱线图的 xaxis 值。