Related
I would like to create a simple plot but with nonstandard breaks.
That's the code for my data:
> dput(dt1)
c(15.9540654816514, 37.5416557213931, 143.317585514018, 317.329051086954,
736.342269565211, 611.759999999995, 1145.49376842104, 3287.57274999997
)
> dput(dt2)
c(7.74957214839424, 17.5499521829522, 47.8167516932271, 72.1468924428822,
131.457629238329, 119.135097468354, 193.812365333332, 339.109355072461
)
> dput(dt3)
c(3.43850794565666, 11.4081262121212, 24.6747108504399, 54.7253625128734,
85.7360432084306, 89.7801271317832, 117.764457806452, 152.859368367347
)
and I would like to achieve something like that:
Just ignore red point on that graph.
That's the code which I have written so far. However, approach of changing the y breaks doesn't work.
plot(dt1,col="blue",cex = 1.8,xlim=c(0,10), ylim = c(1,5000), yaxt = "n", bty="n",xlab="",ylab="")
axis(side = 2, at = C(10,100,1000,5000)
points(dt2,col="green",cex = 1.8)
points(dt3,col="red",cex = 1.8)
Is it possible ? I would like to create identical xlabel like on the attached picture. I can change it as well in other software so do not focus mostly on that.
This is the closest I can think of using ggplot2.
library(data.table)
library(dplyr)
library(ggplot2)
theme_set(theme_bw())
dat <- rbindlist(list(
data.table(dt = "dt1",
y = c(15.9540654816514, 37.5416557213931, 143.317585514018, 317.329051086954,
736.342269565211, 611.759999999995, 1145.49376842104, 3287.57274999997)),
data.table(dt = "dt2",
y = c(7.74957214839424, 17.5499521829522, 47.8167516932271, 72.1468924428822,
131.457629238329, 119.135097468354, 193.812365333332, 339.109355072461)),
data.table(dt = "dt3",
y = c(3.43850794565666, 11.4081262121212, 24.6747108504399, 54.7253625128734,
85.7360432084306, 89.7801271317832, 117.764457806452, 152.859368367347))))
## generate lables
labs <- paste(rep(1:4, c(2,3,2,1)), rep(c(1,2,3,4,3,4), c(1,2,1,1,1,2)), sep = '\n-\n')
## create x variable
dat[, x := rep(1:8, 3) %>% factor(labels = labs)]
## plot
ggplot(dat, aes(x = x, y = y, colour = dt)) +
geom_point() +
scale_y_log10(limits = c(1, 10000),
breaks = 10^(0:4)) +
xlab("") + ylab("")
ggsave('temp.png', width = 4, height = 3)
The output looks like this:
I running into a problem with plotly right now. I'm translating ggplot2 graphs into interactive graphs. Here is an example code:
library(ggplot2)
library(plotly)
x <- sample(x = c(1,2), size = 100, replace = TRUE)
y <- sample(x = c(3,4), size = 100, replace = TRUE)
df <- data.frame(x, y)
df <- transform(df,
x = ifelse(x == 1,"cat","dog"),
y = ifelse(y == 3, "young","old")
)
p <- ggplot(df, aes(x= x, fill = y)) +
geom_bar(position = "stack") +
coord_flip() +
theme(legend.position="none")
ggplotly(p)
I would expect the ggplotly function to create an interactive version but it seems that it's not able to handle factor counts.
ggplot result:
plotly translation:
Any suggestions to solve the issue are welcome!
I'm trying to use cowplot to combine some ggplot2 plots. It should be straightforward, but something in my R or Rstudio surly is broken. What I don't know. I can get it to work with grid.arrange, but the output in my rmarkdown file does not come out as nicely. I broke down my code to the minimum amount to recreate the error, and out of rmarkdown
library(ggplot2)
library(Hmisc)
library(cowplot)
x <- c(1, 8, 9)
y <- c(1, 5, 9)
supply1 <- data.frame(bezier(x, y, evaluation = 500))
g1 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
geom_path(data = supply1, aes(x = x, y = y), size = 1, colour = "BLUE")
g2 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
geom_path(data = supply1, aes(x = x+1.5, y = y+1.5), size = 1, colour = "RED")
plot_grid(g1, g2,
ncol = 2,
nrow = 1)
I get this error:
Error in FUN("text"[[1L]], ...) :
Theme element 'text' has NULL property: margin, debug
I have to detach cowplot, but can get something close with gridExtra using this code:
library(ggplot2)
library(Hmisc)
library(gridExtra)
x <- c(1, 8, 9)
y <- c(1, 5, 9)
supply1 <- data.frame(bezier(x, y, evaluation = 500))
g1 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
geom_path(data = supply1, aes(x = x, y = y), size = 1, colour = "BLUE")
g2 <- ggplot(x = 0:10, y = 0:10, geom = "blank") +
geom_path(data = supply1, aes(x = x+1.5, y = y+1.5), size = 1, colour = "RED")
grid.arrange(g1,g2,
ncol = 2,
nrow = 1)
This code outputs:
grid.arrange plot
Turns out I get the "Error in FUN message" if I try to make any ggplot with both the ggplot2 and cowplot libraries loaded. R 3.1.3, RStudio 0.99.903, cowplot 0.4.0, ggplot2 2.1.0
I have reinstalled everything at least twice, and get the same error situation on a different computer. I can get it to work in a limited fashion. If I wait to call the cowplot library after all other code is run except the plot_grid() chunk, then it will knit and give me the cowplot output. I can not recreate this in a R script only in Rmarkdown, but then I have to have it be the final chunk of the markdown, any ggplot attempts after it will cause the knit to fail.
Short term I used grid.arrange() and just lived with the results, long term I would like to have cowplot as an option.
Any ideas or suggestions?
Apparently it's a bug that has been fixed since R 3.3.1 so upgrade to this version or newer and it should go away.
See example:
I hope I don't need to manually assign the coordinators of the texts. If this is too complicated to achieve in ggplot2, what are the alternatives in R? Or maybe even not in R?
As #Axeman says, ggrepel is a decent option. Unfortunately it will only avoid overlap with other labels, and not the lines, so the solution isn't quite perfect.
library(ggplot2)
install.packages("ggrepel")
library(ggrepel)
set.seed(50)
d <- data.frame(y = c(rnorm(50), rnorm(50, 5), rnorm(50, 10)),
x = rep(seq(50), times = 3),
group = rep(LETTERS[seq(3)], each = 50))
ggplot(d, aes(x, y, group = group, label = group)) +
geom_line() +
geom_text_repel(data = d[d$x == sample(d$x, 1), ], size = 10)
I am fairly new to vegan and ggplot, I have drawn a species diversity plot in vegan. Ggplot has better graph so I was wondering if these codes could be modified to ggplot code.
Any help would be greatly appreciated. I am using bray in vegan.
library(vegan)
library(mass)
data <- read.table("data.txt", header = T)
attach(data)
rownames(data) <- c("TCI1", "TCI2", "TCI3", "TCII1", "TCII2", "TCII3", "TCIII1", "TCIII2", "TCIII3", "TCIV1", "TCIV2", "TCIV3",
"NCI1", "NCI2", "NCI3", "NCII1", "NCII2", "NCII3", "NCIII1", "NCIII2", "NCIII3", "NCIV1", "NCIV2", "NCIV3","TFI1", "TFI2", "TFI3", "TFII1", "TFII2", "TFII3", "TFIII1", "TFIII2", "TFIII3", "TFIV1", "TFIV2", "TFIV3",
"NFI1", "NFI2", "NFI3", "NFII1", "NFII2", "NFII3", "NFIII1", "NFIII2", "NFIII3", "NFIV1", "NFIV2", "NFIV3")
bcdist <- vegdist(data, "bray")
bcmds <- isoMDS(bcdist, k = 2)
plot(bcmds$points, type = "n", xlab = "", ylab = "")
text(bcmds$points, dimnames(data)[[1]])
You can indeed create a plot that looks like the imgur image. First I created some made-up data for your weeds. Then I called ggplot2 and put the weed names at the points, but made the points transparent.
x <- seq(from = -1, to = 1, .025)
df <- data.frame(valuesX = sample(x, size = 48, replace = TRUE),
valuesY = sample(x, size = 48, replace = TRUE),
seeds = c("TCI1", "TCI2", "TCI3", "TCII1", "TCII2", "TCII3", "TCIII1", "TCIII2", "TCIII3", "TCIV1", "TCIV2", "TCIV3",
"NCI1", "NCI2", "NCI3", "NCII1", "NCII2", "NCII3", "NCIII1", "NCIII2", "NCIII3", "NCIV1", "NCIV2", "NCIV3","TFI1", "TFI2", "TFI3", "TFII1", "TFII2", "TFII3", "TFIII1", "TFIII2", "TFIII3", "TFIV1", "TFIV2", "TFIV3",
"NFI1", "NFI2", "NFI3", "NFII1", "NFII2", "NFII3", "NFIII1", "NFIII2", "NFIII3", "NFIV1", "NFIV2", "NFIV3")
)
ggplot(df, aes(x = valuesX, y = valuesY)) +
geom_point(colour = "transparent") +
geom_text(data = df, aes(label = seeds), hjust = 1.5) +
theme_bw() +
labs(x = "Your axis label", y = "", title = "Weed Distribution") +
theme(axis.ticks= element_blank()) +
theme(plot.title = element_text(face = "bold", size = 12))
You can adjust all the elements of the plot as you see fit.