[Solved] ggplot2 confusion

There is nothing wrong that I can see. Your code works. Some times such errors arise due to not flowing the ggplot expression properly. Try it like this: ggplot(train, aes(x= Item_Visibility, y = Item_Outlet_Sales)) + geom_point(size = 2.5, color=”navy”) + xlab(“Item Visibility”) + ylab(“Item Outlet Sales”) + ggtitle(“Item Visibility vs Item Outlet Sales”) Assuming you … Read more

[Solved] Side by side box plot in ggplot2 [closed]

To plot two plots (or more) in one figure, you can use the facet_grid function in ggplot2 (http://docs.ggplot2.org/current/facet_grid.html for more info). You can choose on which trait the plots will be facetted by using a formula. On the left hand side you can mention rows (graphs under each other) and on the left hand side … Read more

[Solved] How can the facet function in ggplot be used to create a histogram in order to visualize distributions for all variables in a dataset?

I’ve copied the data from your post above so you can skip the variable assignment library(“tidyverse”) df <- read.table(file = “clipboard”, header = T) %>% as_tibble() You need to modify your data structure slightly before you pass it to ggplot. Get each of your test names into a single variable with tidyr::gather. Then pipe to … Read more

[Solved] Line graph with ggplot2 in R Studio [closed]

Next time please include the head this can be done using head(Store_sales) ProductID category sales product 1 101 Bakery 9468 White bread 2 102 Personal Care 9390 Everday Female deodorant 3 103 Cereal 9372 Weetabix 4 104 Produce 9276 Apple 5 105 Meat 9268 Chicken Breasts 6 106 Bakery 9252 Pankcakes I reproduced relevant fields … Read more

[Solved] Plotting two scatter plots and regression lines with error bars on same plot using ggplot2 [closed]

ggplot2 is my favourite R package, so here is how I would solve this: df = data.frame(difficulty = 2 + (runif(200) * 6), ID = rep(c(“point”, “bubble”), each = 100)) df$movement = rep(c(1.2, 1.4), each = 100) * df$difficulty + (runif(200) / 5) library(ggplot2) theme_set(theme_bw()) ggplot(df, aes(x = difficulty, y = movement, color = ID, … Read more

[Solved] Why do we use group = group in ggplot2 plots in R?

The example that you give is for plotting maps, usually starting from a shapefile. In that case the data contains a column named group which is used by geom_polygon to ensure that boundaries and shapes are connected correctly. If the column were named something else, e.g. xxx, then you’d use group = xxx. This question … Read more

[Solved] Several questions on ggplot2 [closed]

Have a look at scale_color_manual, the examples should be sufficient. The general structure of tweaking any scale in ggplot2 is to use the appropriate scale function: scale_{aes_name}_{scale_type}, where aes_name can be color, x, or any other aeshetic, and where scale_type can be continuous, discrete, manual, etc. Googling for ggplot2 legend position led me to this … Read more

[Solved] Line chart with categorical values in ggplot2?

1) The issue is that sales_clean$Year is a factor. 2) ggplot interprit your x-value as categorical, y-value as continous and aggregated value into the bar plot (instead bar there are lines). Please see the simulation: library(ggplot2) set.seed(123) sales_clean <- data.frame(Year = rep(factor(2014:2018), 1000), Net_Rev = abs(rnorm(5000))) plotLine <- ggplot(sales_clean, aes(Year, Net_Rev, na.rm = FALSE)) plotLine … Read more

[Solved] how to plot with ggplot?

you should not use all variables as id.vars try this code dfm <- melt(df, id.vars=c(“iteration”)) What do you really need to plot?? What is your x and y axis? line plot or point? As far as i understood this should work. ggplot(dfm,aes(iterartion,value,colour=variable))+geom_point() solved how to plot with ggplot?

[Solved] Stacked bar chart in R [closed]

Please have a look at how to ask questions on SO and how to provide data/examples. It makes it a lot easier for people to help you if we have all the information ready to go. The data I’ve produced a table using some of your data: library(tidyverse) df <- tribble(~absent, ~present, ~total, ~session, 15,8,3,’s1′, … Read more

[Solved] Syntax error in ggplot Function in R

I think it would be less confusing to revert to a for loop. Try: plotTimeSeries <- list(temp1,temp2,temp3,temp4) for (i in plotTimeSeries) { i$dt <- strptime(i$dt, “%Y-%m-%d %H:%M:%S”) ggplot(i, aes(dt, ambtemp)) + geom_line() + scale_x_datetime(breaks = date_breaks(“2 hour”), labels=date_format(“%H:%M”)) + labs(x=”Time 00.00 ~ 24:00 (2007-09-29)”,y=”Ambient Temperature”, title = (paste(“Node”,i))) } 7 solved Syntax error in ggplot … Read more

[Solved] ggplot Grouped barplot with R

You can try library(tidyverse) d %>% gather(key, value, -PtsAgRdTm) %>% ggplot(aes(x=PtsAgRdTm, y=value, fill=key)) + geom_col(position = “dodge”) You transform the data from wide to long using tidyr’s gather function, then plot the bars in a “dodge” or a “stack” way. 3 solved ggplot Grouped barplot with R

[Solved] ggplot chart, x=date, y= value, value

d <- read.table(text=readClipboard(), header=TRUE, stringsAsFactors = T, na.strings=”U”) df <- melt(d, id.var=”date”) ggplot(aes(x=date, y=value), data = df) + geom_bar(aes(fill = variable), stat=”identity”, position = ‘dodge’) or ggplot(aes(x=factor(date), y=value), data = df) + geom_bar(stat=”identity”, position = ‘dodge’) + facet_grid(variable~., scales=”free_y”, drop = F) + theme(axis.text.x = element_text(angle = 45, vjust = 1.1, hjust = 1.05)) 4 … Read more

[Solved] How to add a legend to ggplot when aes are constant for stat_smooth

You can still use aes in each stat_smooth including only the desired color. This will add legends to the plot. Then you can use scale_color_identity to match and rename the colors ggplot(mtcars, aes(x=wt, y = mpg, col=”green”)) + geom_point(col=”blue”) + stat_smooth(method=’loess’,linetype=”dashed”, aes(color = “red”), span=0.1) + stat_smooth(method=’loess’,linetype=”dashed”, aes(color = “orange”), span=0.25) + stat_smooth(method=’loess’,linetype=”dashed”, aes(color = … Read more