我有以下代码:
par(mfrow = c(1,2))
df <- data.frame(countdata = 10000 * runif(10), Modell1 = 30 * runif(10),Modell2=30 * runif(10))
mp=barplot(df$countdata)
lines(mp,df$Modell1,col="red") #implicitno x = 1:10
points(mp,df$Modell1,col="red")
lines(mp,df$Modell2,col="blue") #implicitno x = 1:10
points(mp,df$Modell2,col="blue")
现在的问题是比例级别不同。这意味着我只在x轴上获得一条线。我可以解决这个问题如下:
par(mfrow = c(1,2))
df <- data.frame(countdata = 10000 * runif(10), Modell1 = 30 * runif(10),Modell2=30 * runif(10))
mp=barplot(df$countdata)
par(new=TRUE)
plot(mp,df$Modell1,col="red",type="l",axes=FALSE)
points(mp,df$Modell1,col="red",axes=FALSE)
lines(mp,df$Modell2,col="blue",axes=FALSE) #implicitno x = 1:10
points(mp,df$Modell2,col="blue",axes=FALSE)
正如您现在所看到的,图形中的点被挤压,看起来不那么好。有人可以帮我解决这个问题吗?
顺便说一句,我不想分割或乘以我的数据......(例如Modell1 * 10000)
提前致谢,
费边
答案 0 :(得分:3)
试试这个。您可以通过调整 ylim 来解决问题。
plot(df$countdata, col = "gray", ylim = c(0,11000), xlab = "", ylab = "", ty="h", lend="square", lwd=35, axes = F)
par(new = TRUE)
plot(df$Modell1, col="red", ylim = c(0,200), xlab = "", ylab = "", type="o", pch=19, lwd=2, axes = F)
lines(df$Modell2, col="blue", ty="o", pch=19, lwd=2)
它返回,
答案 1 :(得分:2)
这有点像你正在寻找的吗?
library(tidyverse)
df %>%
mutate(id = row_number()) %>%
ggplot() +
geom_col(aes(id, countdata)) +
geom_point(aes(id, Modell1, col = "Modell1")) +
geom_point(aes(id, Modell2, col = "Modell2")) +
geom_line(aes(id, Modell1, col = "Modell1")) +
geom_line(aes(id, Modell2, col = "Modell2")) +
scale_y_log10() +
scale_x_continuous(breaks = pretty) +
labs(color = "Model") +
theme_minimal()
返回: