我怎样才能用点绘图
how can I plot with a dot
我想知道是否有人可以帮助我以这种方式绘制我的数据,如图所示
只有两种颜色显示来自 df1 或 df2 的数据
Y 轴,比方说两个数据框的第一个值
df1 是
Var1 Freq
1 2 252
并且 df2 是
Var1 Freq
1 2 306
所以,我想代替 Y 轴上的名称,然后是 2,然后是 252 上的一个点(显示 df1 的值)和 306 上的一个点(显示 df2 的值)和之间的黑线二显示两个值之间的距离
两个数据的 Var1 范围不同,所以对于 df1 我们有一些点,而 df2 没有,我们可以只为存在的点绘制一个点
我们根据 Y 轴将数据分为非洲、美洲和亚洲三部分
2 to 10
10 to 20
Higher than 20
我可以用简单的线形甚至频率形状绘制它,但我发现这个图非常困难,我什至无法得到一条线。我将不胜感激
我首先尝试合并失败的数据,因为 Var1 中的某些数据在两个
中都不存在
m=merge(df1,df2,by="Var1")
试试这个:
df <- merge(df1, df2, by='Var1', all=TRUE)
df$Var1 <- as.integer(as.character(df$Var1))
df$Continent <- cut(df$Var1, breaks = c(-Inf, 10, 20, Inf), labels= c('Africa', 'America', 'Asia'))
par(mfrow=c(3,1), mar=c(4,4,1,1), oma=c(1.5,2,1,1))
x.min <- min(c(df$Freq.x, df$Freq.y), na.rm=TRUE)
x.max <- max(c(df$Freq.x, df$Freq.y), na.rm=TRUE)
for (continent in unique(df$Continent)) {
df3 <- df[df$Continent == continent,]
plot(df3$Freq.x, df3$Var1, pch=19, col='red', cex=1.2,
xlim=c(x.min, x.max),
xlab='Freq', ylab=continent)
points(df3$Freq.y, df3$Var1, pch=19, col='blue', cex=1.2)
segments(df3$Freq.x, df3$Var1, df3$Freq.y, df3$Var1, col='gray')
}
title(main="3 Continents of Colonialism", outer=TRUE)
更新了您的新要求:
df <- merge(df1, df2, by='Var1', all=TRUE)
df$Var1 <- as.integer(as.character(df$Var1))
df$Continent <- cut(df$Var1, breaks = c(-Inf, 10, 20, Inf), labels=
c('Africa', 'America', 'Asia'))
par(mfrow=c(3,1), mar=c(4,4,1,1), oma=c(1.5,2,1,1))
x.min <- 0
x.max <- c(300, 50, 10)
xlab <- c('', '', 'Freq')
i <- 1
for (continent in unique(df$Continent)) {
df3 <- df[df$Continent == continent,]
plot(df3$Freq.x, df3$Var1, pch=19, col='red', cex=1.5,
xlim=c(x.min, x.max[i]),
xlab=xlab[i], ylab=continent)
par(xpd=FALSE)
grid (lty = 6, col = "cornsilk2")
par(xpd=TRUE)
points(df3$Freq.y, df3$Var1, pch=19, col='blue', cex=1.5)
segments(df3$Freq.x, df3$Var1, df3$Freq.y, df3$Var1, col='gray', lwd = 2)
i <- i + 1
}
title(main="3 Continents of Colonialism", outer=TRUE)
legend(x=8.6, y=0, legend = c("DF1", "DF2"),
col=c("red", "blue"), pch=19, xpd=NA, bty="o")
使用 ggplot2
library(ggplot2)
ggplot(df, aes(Freq.x, Var1)) +
geom_point(col='red') +
geom_point(aes(Freq.y, Var1), col='blue') +
geom_segment(aes(xend=Freq.y, yend=Var1), col='gray') +
facet_wrap(~Continent, ncol=1, scales = 'free') +
xlab('Freq') +
ylab('Var') +
ggtitle('3 Continents of Colonialism')+
theme_bw()
我想知道是否有人可以帮助我以这种方式绘制我的数据,如图所示
只有两种颜色显示来自 df1 或 df2 的数据
Y 轴,比方说两个数据框的第一个值
df1 是
Var1 Freq
1 2 252
并且 df2 是
Var1 Freq
1 2 306
所以,我想代替 Y 轴上的名称,然后是 2,然后是 252 上的一个点(显示 df1 的值)和 306 上的一个点(显示 df2 的值)和之间的黑线二显示两个值之间的距离
两个数据的 Var1 范围不同,所以对于 df1 我们有一些点,而 df2 没有,我们可以只为存在的点绘制一个点
我们根据 Y 轴将数据分为非洲、美洲和亚洲三部分
2 to 10
10 to 20
Higher than 20
我可以用简单的线形甚至频率形状绘制它,但我发现这个图非常困难,我什至无法得到一条线。我将不胜感激
我首先尝试合并失败的数据,因为 Var1 中的某些数据在两个
中都不存在m=merge(df1,df2,by="Var1")
试试这个:
df <- merge(df1, df2, by='Var1', all=TRUE)
df$Var1 <- as.integer(as.character(df$Var1))
df$Continent <- cut(df$Var1, breaks = c(-Inf, 10, 20, Inf), labels= c('Africa', 'America', 'Asia'))
par(mfrow=c(3,1), mar=c(4,4,1,1), oma=c(1.5,2,1,1))
x.min <- min(c(df$Freq.x, df$Freq.y), na.rm=TRUE)
x.max <- max(c(df$Freq.x, df$Freq.y), na.rm=TRUE)
for (continent in unique(df$Continent)) {
df3 <- df[df$Continent == continent,]
plot(df3$Freq.x, df3$Var1, pch=19, col='red', cex=1.2,
xlim=c(x.min, x.max),
xlab='Freq', ylab=continent)
points(df3$Freq.y, df3$Var1, pch=19, col='blue', cex=1.2)
segments(df3$Freq.x, df3$Var1, df3$Freq.y, df3$Var1, col='gray')
}
title(main="3 Continents of Colonialism", outer=TRUE)
更新了您的新要求:
df <- merge(df1, df2, by='Var1', all=TRUE)
df$Var1 <- as.integer(as.character(df$Var1))
df$Continent <- cut(df$Var1, breaks = c(-Inf, 10, 20, Inf), labels=
c('Africa', 'America', 'Asia'))
par(mfrow=c(3,1), mar=c(4,4,1,1), oma=c(1.5,2,1,1))
x.min <- 0
x.max <- c(300, 50, 10)
xlab <- c('', '', 'Freq')
i <- 1
for (continent in unique(df$Continent)) {
df3 <- df[df$Continent == continent,]
plot(df3$Freq.x, df3$Var1, pch=19, col='red', cex=1.5,
xlim=c(x.min, x.max[i]),
xlab=xlab[i], ylab=continent)
par(xpd=FALSE)
grid (lty = 6, col = "cornsilk2")
par(xpd=TRUE)
points(df3$Freq.y, df3$Var1, pch=19, col='blue', cex=1.5)
segments(df3$Freq.x, df3$Var1, df3$Freq.y, df3$Var1, col='gray', lwd = 2)
i <- i + 1
}
title(main="3 Continents of Colonialism", outer=TRUE)
legend(x=8.6, y=0, legend = c("DF1", "DF2"),
col=c("red", "blue"), pch=19, xpd=NA, bty="o")
使用 ggplot2
library(ggplot2)
ggplot(df, aes(Freq.x, Var1)) +
geom_point(col='red') +
geom_point(aes(Freq.y, Var1), col='blue') +
geom_segment(aes(xend=Freq.y, yend=Var1), col='gray') +
facet_wrap(~Continent, ncol=1, scales = 'free') +
xlab('Freq') +
ylab('Var') +
ggtitle('3 Continents of Colonialism')+
theme_bw()