ggplot2

Sequence x-Axis labels when when 'breaks' has already been defined (R, ggplot)

大憨熊 提交于 2021-02-05 09:28:37
问题 I used scale() function on my data to avoid high correlation when doing a mixed model. Now I want the original values to appear in my plot. So I reversed the scaling with x * attr(x, 'scaled:scale') + attr(x, 'scaled:center') and put the values in a new column of the dataframe that I use to plot. So as an example my data now looks something like this, where x is the real value and x.s the scaled value: x <- sample(x=1:100, size = 50) y <- sample(x=1:100, size = 50) df <- as.data.frame(cbind(x

Sequence x-Axis labels when when 'breaks' has already been defined (R, ggplot)

你说的曾经没有我的故事 提交于 2021-02-05 09:27:25
问题 I used scale() function on my data to avoid high correlation when doing a mixed model. Now I want the original values to appear in my plot. So I reversed the scaling with x * attr(x, 'scaled:scale') + attr(x, 'scaled:center') and put the values in a new column of the dataframe that I use to plot. So as an example my data now looks something like this, where x is the real value and x.s the scaled value: x <- sample(x=1:100, size = 50) y <- sample(x=1:100, size = 50) df <- as.data.frame(cbind(x

regrading adding a legend using ggplot2 for different lines

半城伤御伤魂 提交于 2021-02-05 09:23:27
问题 I want to add a legend that will tell which color represents which line using ggplot2 . My code is as follows : require(lme4) require(ggplot2) m1 <- lmer(Reaction ~ 1+I(Days) + (1+ Days| Subject) , data = sleepstudy) pred1new1=predict(m1,re.form=NA) To add a legend, i tried scale_colour_manual , but it didnt worked. p21 <- ggplot(data = sleepstudy, aes(x = Days, y = Reaction)) p21+ geom_point() + geom_smooth(col="blue")+ geom_line(aes(y=pred1new1,group = Subject) ,col="red", lwd = 0.5)+ scale

scales_fill_continuous doesn't work (ggplot2)

試著忘記壹切 提交于 2021-02-05 08:55:07
问题 I have a data: df_1 <- data.frame( x = replicate( n = 2, expr = rnorm(n = 3000, mean = 100, sd = 10) ), y = sample(x = 1:3, size = 3000, replace = TRUE) ) And the follow function: library(tidyverse) ggplot(data = df_1, mapping = aes(x = x.1, fill = x.1)) + geom_histogram(color = 'black', bins = 100) + scale_fill_continuous(low = 'blue', high = 'red') + theme_dark() scale_fill_continuous doesn't work. The graph is black and gray. Tks. 回答1: The problem, I think, is that there are nrow(df_1)

Removing empty spaces between bars in faceted barplot

流过昼夜 提交于 2021-02-05 08:38:11
问题 I have a plot like this, made from: plot = ggplot(dat, aes(x=Nest)) + geom_bar(aes(fill=MotherID), position = position_stack(reverse = TRUE)) + facet_wrap(~Year) + scale_fill_grey(start = 0, end = .9) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + theme(legend.position="none") + ylab("Number of offpsring") I'd like all the bars to be next to each other without the gaps between them at like 3, 4, 9 on the x axis. I guess it is because nest is a number rather than a string. I have

R: by group mirrored histogram using ggplot2

五迷三道 提交于 2021-02-05 08:27:10
问题 I would like to get a mirrored histogram by group using ggplot as illustrated in the picture at the bottom. library(ggplot2) data2 <- data.frame( type = c( rep("Top 1", n_segment), rep("Top 2", n_segment), rep("Bottom 1", n_segment), rep("Bottom 2", n_segment)), value = c( rnorm(n_segment, mean=5), rnorm(n_segment, mean=12), rnorm(n_segment, mean=-5), rnorm(n_segment, mean=-12)) ) # Represent it data2 %>% ggplot( aes(x=value, fill=type)) + geom_density( color="#e9ecef", alpha=0.6) For now I

Changing the x-axis labels of a ggplot histogram

六月ゝ 毕业季﹏ 提交于 2021-02-05 08:12:22
问题 I have the following dataset (edited for readability): chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE) And I am creating a histogram of the data doing: ggplot(data=chol, aes(chol$AGE)) + geom_histogram() For a particular example I would like to change the x-labels however. Any thoughts on how I can pull this of? 回答1: To illustrate the answer (and better understand the question) a picture: > require(ggplot2) > chol <- read.table(url("http://assets

Stacked bar chart across multiple columns

◇◆丶佛笑我妖孽 提交于 2021-02-05 08:12:15
问题 I managed to transform my initial dataset into this form: ElemId total_count coef_true coef_false coef_ratio 1 a1 2 2 0 1 2 a2 4 4 0 1 3 a3 1 1 0 1 4 a4 5 5 0 1 5 a5 1 1 5 1 6 a6 4 4 0 1 7 a7 4 4 3 1 8 a8 2 2 2 1 9 a9 3 3 1 1 10 a10 1 1 4 1 Right now what am I trying to achieve is to plot stacked bar chart showing the coef_true and coef_false on single bars using ggplot, while preserving order by coef_ratio . Is there a way to do it without further transforming the dataset? EDIT. Modifying

Changing the x-axis labels of a ggplot histogram

。_饼干妹妹 提交于 2021-02-05 08:11:43
问题 I have the following dataset (edited for readability): chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE) And I am creating a histogram of the data doing: ggplot(data=chol, aes(chol$AGE)) + geom_histogram() For a particular example I would like to change the x-labels however. Any thoughts on how I can pull this of? 回答1: To illustrate the answer (and better understand the question) a picture: > require(ggplot2) > chol <- read.table(url("http://assets

R: Parallel Coordinates Plot without GGally

倖福魔咒の 提交于 2021-02-05 08:09:56
问题 I am using the R programming language. I am using a computer that does not have a USB port or an internet connection - I only have R with a few preloaded libraries (e.g. ggplot2, reshape2, dplyr, base R). Is it possible to make "parallel coordinate" plots (e.g. below) using only the "ggplot2" library and not "ggally"? #load libraries (I do not have GGally) library(GGally) #load data (I have MASS) data(crabs, package = "MASS") #make 2 different parallel coordinate plots ggparcoord(crabs)