My code is not generating a monthly trend line graph - r

I have a Date column and Value column. I did my research on internet and tried every possible thing but it does not shows my the trend line graph. I am totally confused what is happening in my data. I have shared my code below:
ggplot(data = New, aes(x = OrderDate, y = TotalAmountWithGST))+
geom_line(color = "#00AFBB", size = 2) + scale_x_date(date_labels = "%b/%Y")
ggplot(x, aes(x = OrderDate, y = TotalAmountWithGST)) +
geom_line()+
theme_minimal()
I am trying to plot a line graph that shows a monthly trend but somehow I am getting a graph that is similar to bar graph but its not a line graph.

You need to add a geom_smooth to your ggplot code.
It's hard to replicate a working example without sample data but that should get you on the right path.

Related

ggplot, geomline not looking right

I am trying to plot my data using ggplot and geomline in R studio but somehow the plot is not looking right. This is my sample data:
Sample data
Here is the code:
ggplot()+
geom_line(data = data_south, mapping = aes(x = Month, y = Tempture), color="blue")
This is the output I am getting:
But this plot is not looking right. The lines are supposed to be connected and also why there are straight lines in the plot?

Jagged plot lines with proper group set (ggplot in R)

I am trying to create line graph with the following code:
ggplot(lnewdat, aes(x = Cl, y = Probability, colour = Level, group = Level)) + geom_smooth(stat = 'identity') + facet_grid(Species~Year)
I get the a graph that looks similar to this graph:
When looking up a solution to the jagged lines, everything mentions setting the "Group". As you can see from my code, I have already done that. Your help is appreciated!

how to plot percentage instead of count, in facet_grid graph?

I am having a hard time plotting percentage instead of count when using facet_grid.
I have the following DF (this is an example, my DF is much longer):
'Gu<-c("1","0","0","0","1","0")
variable<-c("THR","Screw removal","THR","THR","THR","Screw removal")
value<-c("0","1","0","1","0","0")
df2<-data.frame(Gu,variable,value)'
and I am trying to plot the "1" values out of the specific variable (either THR or Screw removal) and split the graph by "Gu" (facet grid).
I manage to code it to plot count, but I can seem to be able to calculate the percentage (I need to calculate the percentage from each variable only and not from all the DF)
This is my code:
ggplot(data = df2, aes(x = variable,y =value ,
fill = variable)) +
geom_bar(stat = "identity")+
facet_grid(~ Gu,labeller=labeller(Gu
=c('0'="Nondisplaced fracture",'1'="Displaced
fracture")))+
scale_fill_discrete(name = "Revision", labels =
c("THR","SCREW"))
and this is what I plotted:
enter image description here
I searched this website and the web and couldn't find an answer...
any help will do!
thanks

How to remove or not show any of the data points above and below the error bar in boxplot and violin plot?

I'm working on a very large dataset containing around 1.6M data points. I'm using the violin plot along with the boxplot to represent the data from each category (there are multiple categories and each has its own set of values).
But the problem which I'm facing is, there are a lot of data points (outliers) above the error bar because of that the focus of the plot has been lost.
Earlier I thought that probably if I remove all the data points after a specific value it will help me to represent what I wanted to show. But It didn't work because for each category the errorbar range is different and because of that, I lost the majority of data from other categories.
So, now I'm thinking to remove or not showing the data points above the error bar for each category individually, for both box and violin plot. And I introduced outlier.shape=NA in the geom_boxplot, it worked fine for the boxplot. Similarly, I wanted to remove all those data points from the violin plot as well which are above the error bar in the boxplot.
Here are the plots before and after using outlier.shape=NA.
Before:
After:
Here is my code :
med_violin <- data %>%
left_join(sample_size) %>%
mutate(myaxis = fct_reorder(paste0(Country), Diff, .fun='median')) %>%
ggplot( aes(x=myaxis, y=Diff, fill=Country)) +
geom_violin(width=1.5, color = "black", position = position_dodge(width=1.8), trim = TRUE) +
geom_boxplot(width=0.2, color="white", alpha=0.01, outlier.colour="red", outlier.size=0.1, outlier.shape = NA) +
scale_y_continuous(breaks = c(0,25,50,75,100,125,150,525,550))+
coord_trans(y = squash_axis(150, 525, 15)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))+
theme(axis.text.x = element_text(size = 8))+
theme(legend.position ="none")+
scale_fill_viridis(discrete = TRUE) +
xlab("")
med_violin
How can I implement the same thing in genom_violin, so that it will also not show the data points above the error bar?
I even tried this : Ignore outliers in ggplot2 geom_violin
But did not work for me.
Thank you.

Can't change the colors on a ggplot2 histogram

I'm working with a dataset of 5k finish times that looks a little bit like this:
"15:34"
"14:23"
"17:34"
and so on, there's a lot, but they're all formatted like that. I'm able to convert all of them to POSIXct, and store them in a data frame to make using ggplot2 easier, but for the life of me, I cannot get ggplot to change colors. The fill command doesn't work, the graph just remains grey.
I've tried just referencing the POSIXct object I made, but ggplot throws an error and tells me it doesn't work well with POSIXct. The only way I've been able to display a histogram is by storing it in a dataframe.
The code I'm currently using looks like:
#make the data frame
df <- data.frame(
finish_times = times_list)
#set the limits on the x axis as datetime objects
lim <- as.POSIXct(strptime(c('2018-3-18 14:15', '2018-3-18 20:00'), format = "%Y-%m-%d %M:%S"))
#making the plot
ggplot(data = df, aes(x = finish_times)) +
geom_histogram(fill = 'red') + #this just doesn't work
stat_bin(bins = 30) +
scale_x_datetime(labels = date_format("%M:%S"),
breaks = date_breaks("1 min"),
limits = lim) +
labs(title = "2017 5k finishers",
x='Finish Times',
y= 'Counts')
I've crawled through a lot of ggplot and R documentation, and I'm not sure what I'm missing, I appreciate all help, thanks
stat_bin(bins = 30) is overriding anything you set in geom_histogram(). Generally, each geom has an associated default stat, and you can plot the object using one of the two, but when you try to do it with both, you can end up with problems. There are several solutions to this. Here's an example.
ggplot(diamonds, aes(x = carat)) + geom_histogram(fill = "red") + stat_bin(bins = 30)
Produces a plot with gray fill
ggplot(diamonds, aes(x = carat)) + geom_histogram(fill = "red", bins = 30)
Produces a plot with red fill

Resources