logo
down
shadow

Re order x-axis in ggplot so time goes from 12AM to 11PM in R


Re order x-axis in ggplot so time goes from 12AM to 11PM in R

By : minhnt1991
Date : November 22 2020, 03:01 PM
wish help you to fix your issue
code :
library(dplyr)

df2 <- df %>% 
    mutate(time = strptime(Hour, format="%I:%M:%S %p") %>% format(., "%H"))

ggplot(df2, aes(time, CrimeFreq, group=1)) + 
       geom_line()
df2 <- df %>% 
      mutate(time = as.POSIXct(strptime(Hour, format="%I:%M:%S %p", tz="Singapore")))

ggplot(df2, aes(time, CrimeFreq, group=1)) + geom_line() +
   scale_x_datetime(date_breaks = "2 hour",
                    labels = function(x) format(x, format = "%I%p", tz="Singapore"))


Share : facebook icon twitter icon
How do I retrieve results from mysql where a timestamp is between 11pm yesterday and 11pm today?

How do I retrieve results from mysql where a timestamp is between 11pm yesterday and 11pm today?


By : Vagner
Date : March 29 2020, 07:55 AM
will help you I have a simple mysql table called messages with the following fields:
code :
select * from table where create_date 
between concat_ws(' ',curdate(),'23:00:00') - interval 1 day 
and
concat_ws(' ',curdate(),'23:00:00') 
How to force ggplot to order x-axis or y axis as we want in the plot?

How to force ggplot to order x-axis or y axis as we want in the plot?


By : MKhalid
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a data frame with the first column is like this , You can try
code :
rn <- rownames(t[t[,1] > 0.02,, drop=FALSE])
tab1 <-  subset(tab, rowname %in% rownames(t)[t > 0.02])
tab1$rowname <- factor(tab1$rowname, levels=rn)

library(ggplot2)

 ggplot(tab1,aes(x = rowname, y = variable, fill = value)) + 
         geom_tile() + 
         scale_fill_gradient2(high="red",mid="white",low="blue") +
         theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) 
 library(dplyr)
 library(tidyr)
 library(ggplot2)

 bind_cols(data.frame(rowname=row.names(df)), df) %>%
    filter(rowMeans(.[-1]) >0.02) %>% 
    gather(variable, value,-rowname) %>%
    mutate(rowname=factor(rowname, levels=rn)) %>%
    ggplot(., aes(x=rowname, y=variable, fill=value))+ 
       geom_tile()+
       scale_fill_gradient2(high='red', mid='white', low='blue')+
       theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
       xlab('x axis') +
       ylab('y axis')
ggplot: How to make the x/time-axis of a time-series plot only the time-component, not the date?

ggplot: How to make the x/time-axis of a time-series plot only the time-component, not the date?


By : Sujata Waghmode
Date : March 29 2020, 07:55 AM
hope this fix your issue We just need a POSIX datetime with all the hours having the same date. The date doesn't matter, pick any you like:
code :
dataframe <- dataframe %>% 
  mutate(hour = strftime(time, format="%H:%M:%S")) %>% 
  group_by(hour) %>% 
  summarize(mean = mean(value)) %>% 
      # add the date back in
  mutate(x_date = ymd_hms(paste("2008-01-01", hour))) %>%
  ungroup()

ggplot(dataframe, aes(x = x_date, y = mean, group = 1)) + 
  geom_line(size = 2) 
Order ggplot barchart by numerical data on x axis, not alphabetical on y axis

Order ggplot barchart by numerical data on x axis, not alphabetical on y axis


By : Dina ElGewaily
Date : March 29 2020, 07:55 AM
hope this fix your issue You don't need to change Crude.Rate to a factor. You can reorder State by Crude.Rate:
code :
ggplot(stDis10, aes(y = Crude.Rate, x = reorder(State, Crude.Rate))) + geom_col() + coord_flip()
Order the x-axis in ggplot based on the exact order in my table

Order the x-axis in ggplot based on the exact order in my table


By : מחמד מוסראתי
Date : March 29 2020, 07:55 AM
help you fix your problem Well, it makes some sense because number of patents is ordered alphabetically. To achieve what you want you need to change the order of levels in factor:
code :
companiesoverpatents$`Number of patents` <- factor(companiesoverpatents$`Number of patents`, levels = unique(companiesoverpatents$`Number of patents`))
Related Posts Related Posts :
  • how to loop for division funciton in r
  • Why does ggplot not allow suppressing of messages generated by its geoms?
  • Download multiple excel files linked through urls in R
  • sparklyr : spark_apply function is not working in cluster mode
  • dplyr mutate - How do I pass one row as a function argument?
  • R selecting rows by conditions given in an external table
  • Native regex way to replace multiple leading chars with equal number spaces
  • stan - difficulty vectorizing
  • How to define a function that calls shiny functions?
  • How to count number of observations in a "n" dimensional range in R
  • Superimposing asymmetric t-distribution using ggplot2
  • Makefile to render all targets of all .Rmd files in directory
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org